You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中FileProvider获取的图片Uri无法上传至AWS S3

Fixing AWS S3 Upload Error with FileProvider URI

The core issue here is that FileProvider returns a content URI (content://), not a direct file system path. When you try to create a File object using imgUri.getPath(), it doesn't point to an actual file on the device's filesystem—this is exactly why you're seeing the "Invalid file" exception from AWS TransferUtility.

Here are two reliable solutions to fix this:


This approach reads the image content directly from the content URI via an InputStream and passes it to TransferUtility, avoiding unnecessary temporary file creation.

Step-by-Step Code Implementation:

  1. Get an InputStream from the content URI using ContentResolver:
InputStream inputStream = null;
try {
    inputStream = getContext().getContentResolver().openInputStream(imgUri);
} catch (FileNotFoundException e) {
    e.printStackTrace();
    // Handle error: Failed to access the image file
}
  1. Create ObjectMetadata to specify key file details (optional but improves upload reliability):
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("image/jpeg"); // Adjust based on your image type (e.g., image/png)

// Optional: Set the file size if you can retrieve it
Cursor cursor = getContext().getContentResolver().query(imgUri, null, null, null, null);
if (cursor != null) {
    int sizeColumnIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
    cursor.moveToFirst();
    metadata.setContentLength(cursor.getLong(sizeColumnIndex));
    cursor.close();
}
  1. Upload using the InputStream overload of TransferUtility:
if (inputStream != null) {
    TransferObserver transferObserver = transferUtility.upload(
        "my_bucket",
        bucketFilePath,
        inputStream,
        metadata,
        CannedAccessControlList.PublicRead
    );
    // Add a TransferListener to track upload progress/status if needed
}

Solution 2: Copy to Temporary File and Upload

If you prefer working with a File object, copy the content from the content URI to a temporary file in your app's private cache directory, then upload that file.

Code Example:

// Create a temporary file in the app's cache directory
File tempUploadFile = new File(getContext().getCacheDir(), "temp_s3_upload.jpg");

// Copy content from the content URI to the temp file
try (InputStream in = getContext().getContentResolver().openInputStream(imgUri);
     OutputStream out = new FileOutputStream(tempUploadFile)) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }
} catch (IOException e) {
    e.printStackTrace();
    // Handle error: Failed to copy the file
}

// Upload the temporary file to S3
TransferObserver transferObserver = transferUtility.upload(
    "my_bucket",
    bucketFilePath,
    tempUploadFile,
    CannedAccessControlList.PublicRead
);

// Optional: Clean up the temp file after upload finishes
transferObserver.setTransferListener(new TransferListener() {
    @Override
    public void onStateChanged(int id, TransferState state) {
        if (state == TransferState.COMPLETED || state == TransferState.FAILED) {
            tempUploadFile.delete();
        }
    }

    @Override
    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {}

    @Override
    public void onError(int id, Exception ex) {}
});

Key Notes:

  • Always handle exceptions (like FileNotFoundException or IOException) to prevent app crashes.
  • For Solution 1, TransferUtility will handle closing the InputStream once the upload completes, but double-check your implementation if you're adding custom listeners.
  • For Solution 2, using getCacheDir() ensures the temporary file is private to your app and will be cleared automatically if the app is uninstalled—no extra storage permissions required.

内容的提问来源于stack exchange,提问作者Mazid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:45:43