[英]FileNotFoundException when picking image from Gallery
I'm building an Android Application that has a button, and when the user click it he has to pick an image, from gallery or camera, that will be sent to my server. 我正在构建一个带有按钮的Android应用程序,当用户单击它时,他必须从图库或照相机中选择一个图像,然后将其发送到我的服务器。 But when I try to test it in the emulator, when I submit I encountered the following error: 但是,当我尝试在模拟器中对其进行测试时,在提交时遇到以下错误:
12-30 17:44:14.435: W/System.err(4216): java.io.FileNotFoundException:
/content:/com.android.providers.media.documents/document/image%3A14: open failed: ENOENT
(No such file or directory)
The error is there: 错误在那里:
FileBody cbFile = new FileBody(this.image, "image/*");
And this.image
is the image that was picked from the gallery. 而this.image
是从画廊挑选的图像。
I have done something similar to this in one of my apps. 我在其中一个应用程序中做了类似的操作。 Try this to see if it works. 试试这个看看是否可行。
This is the code to open the gallery to pick an image... 这是打开图库以选择图像的代码。
Intent intent = new Intent();
intent.setType("image/jpg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
Make sure the following method is in your class because it is called after the gallery activity finishes... 确保以下方法在您的班级中,因为在画廊活动完成后将调用该方法...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
selectedImageUri = data.getData();
try {
Bitmap selectedImage = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), selectedImageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
now you can do whatever you want with the Bitmap selectedImage 现在,您可以使用Bitmap selectedImage进行任何操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.