Ranjan
Ranjan

Reputation: 868

Unable to fetch absolute path of file from android file chooser

I am invoking file chooser with the code below:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 1);

In the onActivityResult method I am creating a File object with the path obtained through

File file=new File(data.getData().getPath());

But I observe that the path returned by the method getPath() is not recognized by File class. As a result, file is never read. I have seen few solution on the web but none of them seem to work. Path looks something like this

content:/com.android.providers.media.documents/document/image%3A15651

Am I missing something?

Am testing on a Samsung Galaxy Note 3 (Android 5.1)

Upvotes: 1

Views: 1584

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006914

I am invoking file chooser with the code below:

That is not a file chooser. It chooses content.

In the onActivityResult method I am creating a File object with the path obtained through

That never worked reliably. It is rather unreliable on Android 4.4 and higher. data, in this case, is a Uri. That Uri points to content, just as the URL in this Web browser points to content. A Uri does not have to point to a file at all, let alone a file that your app can access.

To access the content represented by a Uri, use ContentResolver. In particular, openInputStream() will return an InputStream on the content pointed to by the Uri.

Upvotes: 1

Related Questions