【问题标题】:How to upload images gotten from camera using Android Upload Service如何使用 Android 上传服务上传从相机获取的图像
【发布时间】:2020-04-25 12:34:15
【问题描述】:

我已经能够让用户将照片从他们的图库中上传到服务器,但是当用户使用相机实时捕捉并上传时,我收到此错误不支持的方案:文件:///storage/。 ..

我正在使用android上传服务库

implementation "net.gotev:uploadservice:3.5.2"

我搜了一下发现file://scheme是不允许附加Intent的。

1.点击按钮从相机中选择图像

capture_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, 
 FileProvider.getUriForFile(UploadActivity2.this, BuildConfig.APPLICATION_ID + ".provider", 
 createImageFile()));
                startActivityForResult(intent, 0);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    });

2。获取图像

if (resultCode == Activity.RESULT_OK)
        switch (requestCode){
            case 0:
  try {
                 Uri cameraPath =    FileProvider.getUriForFile(UploadActivity2.this, 
  BuildConfig.APPLICATION_ID + ".provider", createImageFile());
                 String stringUri = cameraPath.toString();
                 selectedImages.add(stringUri);

                }catch (IOException ex) {
                    ex.printStackTrace();
 Glide.with(this)
                        .load(cameraFilePath)
                        .into(display_image);
 break;
        }
     }

如您所见,我正在使用文件提供程序来获取 Uri 并将 uri 存储在名为 selectedimages 的变量中,以便我可以通过意图将其传递给发生上传的另一个活动

createImageFile 方法

  private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.UK).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    //This is the directory in which the file will be created. This is the default location of Camera photos
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM), "Camera");
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    // Save a file: path for using again
    cameraFilePath = "file://" + image.getAbsolutePath();
    return image;
   }

3。传递intent PATH,上传Activity

next_upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(UploadActivity2.this, UploadActivity3.class);
            intent.putStringArrayListExtra("PATH", selectedImages);
            startActivity(intent);
        }
    });

5.上传发生的 UploadAcvity

public void upload() {
    ArrayList<String> selectedImages = getIntent().getStringArrayListExtra("PATH");
try {
        String uploadId = UUID.randomUUID().toString();

        //Creating a multi part request
        new MultipartUploadRequest(this, uploadId, EndPoints.UPLOAD_URL)
                .addFileToUpload(selectedImages.get(0), "image") //Adding file
                .addParameter("caption", captionx) //Adding text parameter to the request
                .setNotificationConfig(new UploadNotificationConfig())
                .setMaxRetries(0)
                .startUpload(); //Starting the upload

    } catch (Exception exc) {
        Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

使用这个。

一个。使用 Glide.with(this) 图像未显示在 point 2 .load(cameraFilePath) .into(display_image);

b.图片上传,但它是一个 0 字节的空文件。

但是当我将 2 点中 selectedImages 的变量值从 selectedImages.add(stringUri); 更改为 selectedImages.add(cameraFilePath);

b.点击上传后,我得到错误 unsupported scheme: file::///storage/

【问题讨论】:

    标签: java android android-studio upload


    【解决方案1】:

    找到答案了。

    1.createImageFile方法中。我必须使用 -

    从捕获的文件中获取内容:// url
    File newFile = new File(storageDir, image.getName());
        contentUrl = FileProvider.getUriForFile(UploadActivity2.this, 
    BuildConfig.APPLICATION_ID + ".provider", newFile);
        stringContentUrl = contentUrl.toString();
    

    2. 然后我将 stringContentUrl 传递给选定的图像字符串,同时获取图像。

    selectedImages.add(stringContentUrl);
    

    3. 通过 Intent Extra 传递它并在 uploadActivity 中调用它,如问题中的 34 点所示。

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2021-12-23
      • 2016-08-20
      相关资源
      最近更新 更多