Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File not found exception (Path from URI)

I'm trying to get a FileInputStream object for the picture a user selects from their gallery and when I'm trying to open the file from the URI I receive, it keeps saying FileNotFoundException...

Here's the code I'm using to fire off the intent for picking an image from the gallery:

Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

And here's the code I use for catching onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();
        File myFile = new File(uri.getPath());
        FileInputStream fiStream = new FileInputStream(myFile);
        ...
    }

}

Right on the creation of the FileInputStream, I get the folloiwng error upon choosing one of my photos via the emulator:

W/System.err: java.io.FileNotFoundException: /document/image:26171: open failed: ENOENT (No such file or directory)

Maybe I'm retrieving the file from the URI incorrectly??? Any help here would be greatly appreciated, thanks!!

Solution

I ended up using https://android-arsenal.com/details/1/2725 It's very easy to use!

like image 881
Riptyde4 Avatar asked Mar 31 '16 09:03

Riptyde4


1 Answers

The issue here is that you are attempting to access a content Uri as though it were a File.

The Uri that you are attempting to open is content://document/image:26171. You need to access it with a ContentProvider.

An example to do so can be found with this great Stack Overflow answer.

like image 199
Knossos Avatar answered Sep 17 '22 17:09

Knossos