SagiLow
SagiLow

Reputation: 6029

Getting File or full path from Content URI returned on OnActivityResult

I want the user to select any file type, I use the following code to do so:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); // TODO filter types
startActivityForResult(intent, CONVERSATION_ATTACHMENT_REQ_CODE);

However, on onActivityResult I try to get the file path using data.getDataString but I get content:// uri.
How do I get the absolute file path from this uri ?
Will I always get content:// uri's or I need to handle different data types ?

Upvotes: 1

Views: 1905

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006724

How do I get the absolute file path from this uri ?

You don't, just as you do not get an absolute path on your hard drive for https://stackoverflow.com/questions/33576566/getting-file-or-full-path-from-content-uri-returned-on-onactivityresult (the URL for this question) from the URL itself. Neither a URL nor a Uri is a file.

Use a ContentResolver, and methods like openInputStream(), to consume the content identified by that Uri.

Will I always get content:// uri's or I need to handle different data types ?

You will get whatever the activity that the user chooses to handle your implicit Intent elects to return. That's supposed to be content: Uri values. It might be file: Uri values, though ContentResolver can handle those as well.

Upvotes: 1

Related Questions