简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM