androidstorage-access-frameworkdocumentfile

Explore contents of Uri Folder retrieved from Intent.ACTION_OPEN_DOCUMENT


I want to be able to access the list of files from a DocumentFile, when the user selects a folder instead of a file. I am calling the intent like this:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setFlags(FLAG_READ_WRITE|FLAG_PERSIST );
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType(TYPE_ANY);
    String [] mimeTypes = {TYPE_IMAGE,TYPE_VIDEO};
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
    startActivityForResult(intent, REQUEST_CODE_SELECT_VIDEOS);

(neverming the FLAG_... and TYPE_... they are what you would expect for their names) then, in onActivityResult:

if (resultCode == Activity.RESULT_OK) {               
          if (resultData != null) {
                Uri uri = resultData.getData(); // try to get only one element
                if (uri!=null){ // data is available as ClipData because multiple items were selected
                    Log.i(TAG, "Uri: " + uri.toString());
                    selectedDocs.add(uri);
                }else{
                    ClipData data = resultData.getClipData();
                    for(int i=0;i<data.getItemCount();i++){
                        ClipData.Item item = data.getItemAt(i);
                        Uri aUri = item.getUri();
                        selectedDocs.add(aUri);
                    }
                    Log.i(TAG, "handleReadRequestResult: data=" + data.toString());
                }
            }else{
                Log.e(TAG, "handleReadRequestResult: resultData is null");
                handleIssues();
            }
        }else{
            Log.e(TAG, "handleReadRequestResult: resultCode is not OK");
            handleIssues();
        }

However, when I try to read the uris, there are two cases: (i) current Uri "is a file", and (ii) curent Uri "is a Folder"

EDIT: as I have been told, case (ii) is not the default behavior. I can reproduce this by longtapping a folder (multiple-selection mode ? ) –

in case (i), I can do this: DocumentFile.fromSingleUri(getApplicationContext(),uri), and work with the stuff. the problem is in case (ii), where I would like to list available files.

NOTE: when calling new Intent(Intent.ACTION_OPEN_DOCUMENT), I get uri.toString()=content://com.android.externalstorage.documents/document/BEAE-19F8%3AFolderName, but with new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), I get uri.toString()=content://com.android.externalstorage.documents/tree/BEAE-19F8%3AFolderName.

the difference here is that when the intent is a Intent.ACTION_OPEN_DOCUMENT_TREE, I can do DocumentFile df = DocumentFile.fromTreeUri(getApplicationContext(),uri), and then df.listFiles (which Will fail for the other case)


Solution

  • It is ugly, but it seems to do the trick for me ( I dont know the consequences it'll have though):

    DocumentFile df = DocumentFile.fromTreeUri(getApplicationContext(),Uri.parse(uri.toString().replace("/document/", "/tree/")))
    

    no complaints!! I can even do df.listFiles() (which is what I wanted all along!)

    EDIT: complain Nº1: persistable uri permissions for that folder are required