简体   繁体   English

从库中选取图像时出现FileNotFoundException

[英]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.

相关问题 从库中选取图像时发生FileNotFoundException - FileNotFoundException while picking images from gallery 在Android应用程序中从图库中选取图像 - image picking from gallery in android application 在Sony z2上从图库中选取图像时发生IllegalArgumentException - IllegalArgumentException while picking image from gallery on Sony z2 从android图像库中选择并将其加载到另一个活动中不起作用 - picking from android image gallery and loading it into another activity not working 从图库中选取图片会导致Android N错误 - Picking an image from gallery gives error Android N 从内部存储中获取图像时,FileNotFoundException - FileNotFoundException when fetch image from Internal Storage 尝试从Web下载图像时出现FileNotFoundException - FileNotFoundException when trying to download a image from web 从图库中选取图像后,android应用程序崩溃-cursor.getColumnIndex返回-1 - android app crashes after picking a image from gallery - cursor.getColumnIndex returns -1 从图库中选择图片时获取NullPointerException - Getting NullPointerException while picking Picture From Gallery Android:尝试从 Uri 获取图像时出现 FileNotFoundException - Android : FileNotFoundException when trying to get an image from Uri
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM