1

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)

pwoolvett
  • 524
  • 4
  • 13
  • The user cannot select a folder with Intent.ACTION_OPEN_DOCUMENT. So your problem description is wrong. – greenapps May 26 '17 at 16:06
  • If `ACTION_OPEN_DOCUMENT` is returning "folders" (i.e., a "document" that really is a document tree), IMHO that's a bug. It should only allow users to select documents, even in multiple-selection mode. – CommonsWare May 26 '17 at 16:08
  • I can produce this behavior by longtapping a folder (multiple-selection mode ? ) – pwoolvett May 26 '17 at 16:11
  • Please put that info directly in your post. You should have done that right away. I will investigate. – greenapps May 26 '17 at 16:17
  • It is indeed Intent.EXTRA_ALLOW_MULTIPLE that allows you to select a folder with Intent.ACTION_OPEN_DOCUMENT. It's just as you said it is. Never seen that before. What to do with it? What can be done with it? – greenapps May 26 '17 at 16:31
  • One can select multiple folders and files then. – greenapps May 26 '17 at 16:50
  • @greenapps yes, but as I mentioned in the NOTE, the intent is ACTION_OPEN_DOCUMENT, so the repective Uri for the folder cant (or at least I dont know how to) be used to `listFiles()`... – pwoolvett May 26 '17 at 16:55
  • I know that it is action OPEN_DOCUMENT. And as the delivered uri is a wrong one for a folder just dont use it. What is wrong with ACTION_OPEN_DOCEMENT_TREE? – greenapps May 26 '17 at 17:02
  • example: root: {file A1, file A2, dir B}; dir B: {file B1, file B2} I want the user to be able to select several files at once (hence the `EXTRA_ALLOW_MULTIPLE`), but as on my test I had some files on the root and some other on subfolders, I thought any user could've selected both a file and a folder. Maybe the user does not want all files inside the root folder (eg he wants A1,B1,B2) – pwoolvett May 26 '17 at 17:11
  • You better ask the user why he selected a folder. You offered him to select a file or multiple files isnt it? Its a bug. Dont build your app on it! It will bite you sooner or later. – greenapps May 26 '17 at 17:16

1 Answers1

1

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

pwoolvett
  • 524
  • 4
  • 13