我有一个带有android 5.0的nexus 5。 我想将图像发送到亚马逊s3但我收到此错误:
11-27 11:29:52.687 21423-21423/com.laurenswuyts.witpa E/Upload to amazons3﹕ com.amazonaws.AmazonClientException: Unable to calculate MD5 hash: /document/image:2810: open failed: ENOENT (No such file or directory)
at com.amazonaws.services.s3.AmazonS3Client.putObject(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.uploadInOneChunk(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.call(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.upload(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(Unknown Source)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.io.FileNotFoundException: /document/image:2810: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.amazonaws.util.Md5Utils.computeMD5Hash(Unknown Source)
at com.amazonaws.util.Md5Utils.md5AsBase64(Unknown Source)
at com.amazonaws.services.s3.AmazonS3Client.putObject(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.uploadInOneChunk(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.call(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.upload(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(Unknown Source)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.amazonaws.util.Md5Utils.computeMD5Hash(Unknown Source)
at com.amazonaws.util.Md5Utils.md5AsBase64(Unknown Source)
at com.amazonaws.services.s3.AmazonS3Client.putObject(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.uploadInOneChunk(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.call(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.upload(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(Unknown Source)
at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(Unknown Source)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
所以我有一个按钮,您可以从手机中选择图像:
//When clicked on addimage
addImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// To open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
uri = data.getData();
//create file of image to upload
images3 = new File(uri.getPath());
}
}
我在line = images3 = new File(uri.getPath());
中得到错误要将我上传的完整代码发送到亚马逊s3:
TransferManager transferManager = new TransferManager(credentialsProvider);
try {
mUpload = transferManager.upload(
"witpalol",
"image.jpg",
images3);
mUpload.waitForCompletion();
} catch (Exception e) {
Log.e("Upload to amazons3", "", e);
}
但问题是我无法获取文件...... 有人有解决方案吗?
修改
我试图获得uri的完整路径:
// And to convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
// can post image
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
所以images3 =新文件(getRealPathFromURI(uri));
但是我在那一行得到了这个错误:
11-27 11:55:18.872 23286-23286/com.laurenswuyts.witpa E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.laurenswuyts.witpa, PID: 23286
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:2810 flg=0x1 }} to activity {com.laurenswuyts.witpa/com.laurenswuyts.witpa.Activities.Events.AddEventActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at com.laurenswuyts.witpa.Fragments.Events.AddEventFragment.decodeUri(AddEventFragment.java:254)
at com.laurenswuyts.witpa.Fragments.Events.AddEventFragment.onActivityResult(AddEventFragment.java:213)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
at android.app.Activity.dispatchActivityResult(Activity.java:6135)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)