无法在aws s3

时间:2018-05-23 11:01:15

标签: android

无法将图片路径上传到android

中的s3
content:/com.earlysalary.android.provider/external_files/IMG_20180523_164228_1998498766.jpg

我正在使用FileProvider来获取相机图像。 尝试上传uri上的S3时获取无效文件异常。

String authorities = mContext.getPackageName();
Uri  imgUri = FileProvider.getUriForFile(getContext(), authorities +".provider", photoFile);

要在s3上传我正在使用

 File uploadFile = new File(imgUri .getPath());

   TransferObserver transferObserver = transferUtility.upload("my_bucket", bucketFilePath, uploadFile, CannedAccessControlList.PublicRead);{

}

收到错误

System.err: java.lang.IllegalArgumentException: Invalid file: content:/com.example.android.provider/external_files/IMG_20180523_164228_1998498766.jpg
05-23 16:43:45.931 30128-30128/com.earlysalary.android W/System.err:     at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:532)
05-23 16:43:45.931 30128-30128/com.earlysalary.android W/System.err:     at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:497)
05-23 16:43:45.931 30128-30128/com.earlysalary.android W/System.err:     at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:436)

1 个答案:

答案 0 :(得分:0)

确保文件路径有效且不为空。另外,请检查访问s3存储桶的正确权限。这个过程大多是直截了当的。点击此处的链接:http://javasampleapproach.com/android/uploaddownload-files-images-amazon-s3-android

在上面的链接中,如果您转到5.4上传文件,请在代码中交叉检查您正在设置正确的TransferObserver。上面链接的代码段如下:

// KEY and SECRET are gotten when we create an IAM user above
BasicAWSCredentials credentials = new BasicAWSCredentials(KEY, SECRET);
AmazonS3Client s3Client = new AmazonS3Client(credentials);

TransferUtility transferUtility =
        TransferUtility.builder()
                .context(getApplicationContext())
                .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                .s3Client(s3Client)
                .build();

// "jsaS3" will be the folder that contains the file
TransferObserver uploadObserver =
        transferUtility.upload("jsaS3/" + fileName, file);

uploadObserver.setTransferListener(new TransferListener() {

   @Override
   public void onStateChanged(int id, TransferState state) {
      if (TransferState.COMPLETED == state) {
         // Handle a completed download.
      }
   }

   @Override
   public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
         float percentDonef = ((float)bytesCurrent/(float)bytesTotal) * 100;
         int percentDone = (int)percentDonef;
   }

   @Override
   public void onError(int id, Exception ex) {
      // Handle errors
   }

});

// If your upload does not trigger the onStateChanged method inside your
// TransferListener, you can directly check the transfer state as shown here.
if (TransferState.COMPLETED == uploadObserver.getState()) {
   // Handle a completed upload.
}