Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I want to get a file path through open a file choose by startActivityForResult which intent is Intent.ACTION_GET_CONTENT and setType(* / *), but when I choose open form the "Nexus 5X" item the return uri is "com.android.externalstorage.documents", how to handle this type uri.
There are some codes.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.ACTION_DEVICE_STORAGE_OK, true);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setType("*/*");
startActivityForResult(intent, FILE_ADD_ACTION_REQUEST_CODE);
screenshot
–
External Storage URIs are of the following form:
content://com.android.externalstorage.documents/root%3Apath
where root
is the root of the storage medium, %3A
is simply an escaped colon and path
is the filesystem path relative to the root (also escaped).
On devices with emulated primary storage (i.e. modern Android devices), the root for primary storage (i.e. /sdcard
) is usually called primary
. In other cases it seems to be the media ID (4+4 hex digits separated by a hyphen).
You can also try using this (requires API 21 for full functionality):
public static String getRealPathFromURI_API19(Context context, Uri uri) {
String filePath = "";
// ExternalStorageProvider
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(':');
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
//getExternalMediaDirs() added in API 21
File[] external = context.getExternalMediaDirs();
if (external.length > 1) {
filePath = external[1].getAbsolutePath();
filePath = filePath.substring(0, filePath.indexOf("Android")) + split[1];
} else {
filePath = "/storage/" + type + "/" + split[1];
return filePath;
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.