1

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)

Ranjan
  • 858
  • 2
  • 10
  • 17

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the explanation. Came across the solution here http://stackoverflow.com/a/25302933/3900531 – Ranjan Feb 23 '16 at 09:04
  • @Ranjan: That will only work if the user happens to choose content from an app that returns a `Uri` from the `MediaStore`. That code will break on anything else, such as a `Uri` returned from Google Drive, Dropbox, or any other app that elects to offer an `ACTION_GET_CONTENT` activity. – CommonsWare Feb 23 '16 at 12:00