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

one of the complete uri is "content://com.android.externalstorage.documents/document/home%3Ademo_decry.mp4" – sohnyi May 28, 2017 at 13:27

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;
                Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
                Jan 23, 2018 at 11:06
                I’ve edited the answer, adding an introduction and dropping irrelevant (to the question) portions of the code.
– user149408
                Mar 13, 2019 at 23:47
        

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.