HBiSoft / PickiT

An Android library that returns real paths from Uri's

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unsupported Uri when user choose a directory

shynline opened this issue · comments

I get Unsupported Uri for : content://com.android.providers.downloads.documents/tree/msd%3A11526
when I choose a directory
API 29
And I launch directory picker like this (in order to reproduce)

val intent = Intent(ACTION_OPEN_DOCUMENT_TREE).apply {
putExtra(Intent.EXTRA_LOCAL_ONLY, true)
putExtra(EXTRA_SHOW_ADVANCED, true)
}
fragment.startActivityForResult(intent, requestId)

"ACTION_OPEN_DOCUMENT_TREE" is currently not supported by this library.

What do you expect the library to return when you pass a DocumentFile? Do you want the path of all the files in the selected directory to be returned?

No the path of the directory itself
Like /some_storage/download/the_folder
I want to pass it to File

I will not be implementing this becuase the only reliable way of getting the removable disk id requires API 24>.

Here is an example of how you can achieve this. I know it might not be the cleanest solution, but I've tested it and it works.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("Test Path", getPath(data.getData()));
}

private String getPath(Uri uri) {
    // Internal storage (primary storage)
    String replaceChars = String.valueOf(uri).replace("%2F", "/").replace("%20", " ").replace("%3A",":").replace("tree/","");
    if(replaceChars.contains("primary:")){
        String root = "/storage/emulated/0/";
        return root+replaceChars.substring(replaceChars.indexOf("primary")).replace("primary:","");
    }
    // Downloads directory
    if(replaceChars.contains("raw:")){
        return replaceChars.substring(replaceChars.indexOf("raw")).replace("raw:","");
    }
    // Removable storage (SD Card)
    // You might first want to check if a removable storage is mounted before calling this
    String sdID = getSdId();
    if(sdID != null && replaceChars.contains(sdID)){
        String root = "/storage/"+sdID+"/storage/emulated/0/";
        return root+replaceChars.substring(replaceChars.indexOf(sdID)).replace(sdID+":","");
    }
    return replaceChars;
}
// Get removable storage id
// getStorageVolumes() requires API 24
private String getSdId(){
    String id = "";
    StorageManager storage= getSystemService(StorageManager.class);
    List<StorageVolume> volumes=storage.getStorageVolumes();
    for (StorageVolume volume : volumes) {
        if (volume.getUuid()!=null){
            id = volume.getUuid();
        }
    }
    return id;
}

The above with return this -

Internal Storage (Primary):
/storage/emulated/0/SomeFolder

Download:
/storage/emulated/0/Download/SomeFolder

Removable Storage (SD Card):
/storage/6330-6333/storage/emulated/0/Aaaaaaaa


I did not do a comprehensive test, please let me know if it works for you.
I will be closing this issue since I will not be implementing this into the library.