简体   繁体   中英

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 ?

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.

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