首页 > 解决方案 > 如何接收相机数据以上传到android中的php服务器

问题描述

我正在尝试创建 android 文件上传到 php 服务器,一切正常,除了当我选择相机拍摄上传图片时它没有接收到数据,我已经尝试了所有我可以但无法让它写入的东西。我正在使用 android gotev https://github.com/gotev/android-upload-service进行文件上传。

我认为我的问题是从onActivityResult接收凸轮数据,但我不知道该怎么做。

private Bitmap bitmap;
private Uri filePath;
private String CAM_MESSAGE;
private ValueCallback<Uri> FILE_MESSAGE;

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode == IMAGE_REQUEST_CODE){
           if (resultCode == FileUploadActivity.RESULT_OK && data != null && data.getData() != null) {
               filePath = data.getData();
               try {
                   bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                   tvPath.setText("Path: ".concat(getPath(filePath)));
                   imageView.setImageBitmap(bitmap);
               } catch (IOException e) {
                   e.printStackTrace();
               }
           } else {
               Uri[] camfilePath = null;
               if (CAM_MESSAGE != null) {
                   try {
                       camfilePath = new Uri[]{Uri.parse(CAM_MESSAGE)};
                       bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), camfilePath);
                       tvPath.setText("Path: ".concat(getPath(camfilePath)));
                       imageView.setImageBitmap(bitmap);
                       //Toast.makeText(getApplicationContext(), "CAM MESSAGE:"+CAM_MESSAGE, Toast.LENGTH_SHORT).show();
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               }else{
                   Toast.makeText(getApplicationContext(), "Empty data", Toast.LENGTH_SHORT).show();
               }
           }
       }else{
           Toast.makeText(getApplicationContext(), "Not Request Code", Toast.LENGTH_SHORT).show();
       }
    }

我的文件选择器

private void showFileChooser(){
    File  photoFile = null;
    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
    contentSelectionIntent.setType(APP_FTYPE);
    Intent[] intentArray;
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(FileUploadActivity.this.getPackageManager()) != null) {
        try {
            photoFile = createImage();
            takePictureIntent.putExtra("PhotoPath", CAM_MESSAGE);
        } catch (IOException ex) {
            Log.e(TAG, "Image file creation failed", ex);
        }
        if (photoFile != null) {
            CAM_MESSAGE = "file:" + photoFile.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        } else {
            takePictureIntent = null;
        }
    }
    if (takePictureIntent != null) {
        intentArray = new Intent[]{takePictureIntent};
    } else {
        intentArray = new Intent[0];
    }

    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
    startActivityForResult(chooserIntent, IMAGE_REQUEST_CODE);
}

标签: phpandroidfile-uploadonactivityresult

解决方案


推荐阅读