0

I want to list only files of certain Folder. Im Using Intent.ACTION_OPEN_DOCUMENT_TREE to let the user pick the folder. the URI is safed in sharedPrefs.

Then im trying to get the children from that URI by using

val childrensUriUsingTree = DocumentsContract.buildChildDocumentsUriUsingTree(
            uri,
            DocumentsContract.getTreeDocumentId(uri)
        )

the problem is that the URI childrensUriUsingTree returned by the DocumentsContract does not match the given URI IF the user selected a sub-subfolder. if he uses a 1st level subfolder like Downloads everything works. But if the user selects a sub-subfolder like DCIM/MyFolder that is stripped from the url and only the 1st level subfolder persists.

for instance content://com.android.externalstorage.documents/tree/primary:DCIM/Camera will be made into content://com.android.externalstorage.documents/tree/primary%3ADCIM/document/primary%3ADCIM/children so the Camera part is just stripped.

Which leads to a permission exception because Intent.ACTION_OPEN_DOCUMENT_TREE did not grant access to the 1st level subfolder. Or if permission exists it will list the files from that 1st level subfolder.

Is that a Bug or am i doing something wrong?

babuntu
  • 27
  • 8
  • If you get persistant uri permission from ACTION_OPEN_DOCUMENT_TREE you can always list for all subfolders of choosen folder too. So yes your code is not complete. – blackapps Jul 20 '20 at 14:17
  • I suggest you try to do the listing already in onActivityResult without first saving your uri in shared preferences. Please post that code. – blackapps Jul 20 '20 at 14:21
  • `problem is that the URI childrensUriUsingTree returned by the DocumentsContract does not match the given URI IF the user selected a sub-subfolder. ` Such a children uri never is equal to the parent. But why dont you give an example to make all clear? – blackapps Jul 20 '20 at 14:44
  • for instance `content://com.android.externalstorage.documents/tree/primary:DCIM/Camera` will be made into `content://com.android.externalstorage.documents/tree/primary%3ADCIM/document/primary%3ADCIM/children` so the `Camera` part is just stripped. – babuntu Jul 20 '20 at 15:18

0 Answers0