0

I'm trying to switch to the DocumentFile API as Android doesn't allow using File API anymore...

When I select a folder, it returned URI is

content://com.android.externalstorage.documents/tree/primary%3AMedia%2FCamera%2FFreeTime

Now I'm calling

 DocumentFile.fromTreeUri(this, uri)

to get the content of the folder. I want to keep a list of all the subfolders within

The 1st subfolder I found has the following URI

content://com.android.externalstorage.documents/tree/primary%3AMedia%2FCamera%2FFreeTime/document/primary%3AMedia%2FCamera%2FFreeTime%2F11

I need to save it so later on I can check the files within that folder

The problem is that when I try to call

 DocumentFile.fromTreeUri(this, uri)

on this subfolder uri it returns a DocumentFile matching the parent folder of this subfolder

content://com.android.externalstorage.documents/tree/primary%3AMedia%2FCamera%2FFreeTime

How can I save the subfolder uri here so I can later on list this subfolder files?

user1026605
  • 1,633
  • 4
  • 22
  • 58

1 Answers1

0

How can I save the subfolder uri here so I can later on list this subfolder files?

Two things.

First: take persistable uri permission on the folder choosen by the user.

Second: a subfolder uri as you save it is no tree uri and hence you cannot create a DocumentFile with .fromTreeUri().

Instead use:

 .fromSingleUri().
blackapps
  • 8,011
  • 2
  • 11
  • 25
  • I already tried that, but tha's not helpful as any call to listFiles on the DocumentFile created by .fromSingleUri is throwing an exception – user1026605 Nov 02 '21 at 18:49
  • And yes the permission has been made persistable – user1026605 Nov 02 '21 at 18:49
  • Which exception? – blackapps Nov 02 '21 at 18:50
  • UnsupportedOperationException – user1026605 Nov 02 '21 at 18:55
  • This is normal based on the listFiles javadoc Throws: UnsupportedOperationException – when working with a single document created from fromSingleUri(Context, Uri). – user1026605 Nov 02 '21 at 18:57
  • Shame on me. Sorry. fromSingleUri() is for files. (Time ago i was busy with this.) So save the uri obtained from ACTION_OPEN_DOCUMENT_TREE and make the listing when needed. Just make your own function that takes a tree uri and a subfolde name; But... why not let the user choose the subfolder directly? – blackapps Nov 02 '21 at 19:22
  • Those APIs are so restricted comapred to the File API... and they are so slow... When you have hundreds of subfolders selecting them one by one wouldn't make sense – user1026605 Nov 02 '21 at 20:12
  • Using DocumentFIle is slow. About 20 times as slow as using File class. But if you wanna use SAF dont use DocumentFile. Use DocumentsContract. Its about as fast as the FIle class. – blackapps Nov 02 '21 at 20:14
  • When looking at the code of listFiles() I can see that it's using DocumentContracts so I don't understand how using DocumentContracts directly would be faster. – user1026605 Nov 02 '21 at 20:48
  • No need to understand. Just try and see. And your s is on the wrong place. – blackapps Nov 03 '21 at 09:10
  • Please provide me a sample that is doing this quicker than listFiles() is because the code I end up writing with DocumentsContract is the same as the one they ars using for the listFiles implementation... – user1026605 Nov 03 '21 at 20:31
  • Then please post your code. (In your post). – blackapps Nov 03 '21 at 20:43