从uri创建的文件不能被transferutility占用

时间:2019-04-30 22:11:01

标签: android amazon-s3 awss3transferutility

我正在尝试使用TransferUtility将文件上传到S3。我通过ACTION_OPEN_DOCUMENT目的获得Uri。我得到一个uri,从中创建一个文件。但是s3 transferutility给出了invalidArgumentsException

uri = resultData.getData();
File file = new File(uri.getPath());
final String split[] = file.getPath().split(":");
String file_path = split[1];
File fp = Environment.getExternalStorageDirectory();
File f = new File(fp,file_path);

AmazonS3 s3Client = new AmazonS3Client(new BasicAWSCredentials(my_access_id,my_secret_key));
s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));
TransferUtility transferUtility = new TransferUtility(s3Client, getApplicationContext());
TransferObserver transferObserver = transferUtility.upload(bucket,"samp.png",f);
if (TransferState.COMPLETED == transferObserver.getState()) {
Toast.makeText(getApplicationContext(),"Transfer complete",Toast.LENGTH_SHORT).show();
                }

发生以下错误:

2019-05-01 03:19:26.339 16490-16490/com.example.filepicker E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.filepicker, PID: 16490
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=42, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/document/AE5C-19EC:Screenshot_2019-05-01-02-38-14-06.png flg=0x43 }} to activity {com.example.filepicker/com.example.filepicker.MainActivity}: java.lang.IllegalArgumentException: Invalid file: /storage/emulated/0/Screenshot_2019-05-01-02-38-14-06.png
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4605)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4649)
        at android.app.ActivityThread.-wrap19(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:197)
        at android.app.ActivityThread.main(ActivityThread.java:7022)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:515)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:837)
     Caused by: java.lang.IllegalArgumentException: Invalid file: /storage/emulated/0/Screenshot_2019-05-01-02-38-14-06.png
        at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:532)
        at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:497)
        at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:466)
        at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.upload(TransferUtility.java:407)
        at com.example.filepicker.MainActivity.onActivityResult(MainActivity.java:71)
        at android.app.Activity.dispatchActivityResult(Activity.java:7575)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4601)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4649) 
        at android.app.ActivityThread.-wrap19(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:197) 
        at android.app.ActivityThread.main(ActivityThread.java:7022) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:515) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:837)

0 个答案:

没有答案