2

If I try to get all DocumentFile with DocumentFile.fromTreeUri() it returns me only DocumentFiles on the first level of the Uri without the ones in the subfolders.

If in order to get also all DocumentFile in sub folders, I try to check what DocumentFile is a directory, recursively passing its Uri in the method that uses DocumentFile.fromTreeUri(), the whole procedure is extremely slow.

Is there a better way to do this (without methods that use classic storage access)?

EDIT

The approach above, actually it's not simply slow, but totally stuck. This due to the fact the DocumentFile objects of folders return always the root file list rather than their content list as supposed due to a weird SAF behavior explained here and this caused a loop.

AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • 1
    I suspect that there is no good way to avoid that. If there is, it would [involve using `DocumentsContract` more directly](https://commonsware.com/blog/2019/11/23/scoped-storage-stories-documentscontract.html). `DocumentFile` is a convenience wrapper around queries and other calls made to `DocumentsContract` via a `ContentResolver`. – CommonsWare Jun 14 '20 at 13:30
  • Yes DocumenFile is slow. Still i advise to use it first as it is not that difficult. Then after a while when one sits down to take a rest and complains about slowness we advice to use DocumentsContract. It is up to twenty times as fast as DocumentFile. And about as fast as when using the File class. – blackapps Jun 14 '20 at 13:55
  • In what way can I achieve what described above with `DocumentsContract`? – AndreaF Jun 14 '20 at 14:02
  • @blackapps I have tried and actually this SAF seems much more messed than what I could have initially imagined. Is this a good implementation? :/ https://stackoverflow.com/questions/62375696/unexpected-behavior-when-documentfile-fromtreeuri-is-called-on-uri-of-subdirec – AndreaF Jun 14 '20 at 23:46
  • Yes i saw you were inspired by the f word ;-). But No, as it uses DocumentFile. – blackapps Jun 15 '20 at 08:00
  • Look at the code for void traverseDirectoryEntries(Uri rootUri) in https://stackoverflow.com/questions/41096332/issues-traversing-through-directory-hierarchy-with-android-storage-access-framew It gets you started with listing fast. – blackapps Jun 15 '20 at 08:09
  • `due to the fact the DocumentFile objects of folders return always the root file list `. No. Then you are doing it wrong. – blackapps Jun 15 '20 at 08:12
  • @blackapps With that code the problem remains, I have already tried it, and not even with any of the edit suggested in answers works. (And btw in `listFiles()`is called `buildChildDocumentsUri` too, but any DocumentFile created with child Uri returns me always data of the dad, and if I try to format the Uri manually as a non-Child to try to avoid this weird behavior it returns me a security exception). If I'm doing something wrong show me how create `DocumentFile` from subfolders that list their subcontent programmatically, it should be a very easy task if really SAF is good as you say. – AndreaF Jun 15 '20 at 10:05
  • Show your recursive function using DocumentFile. Show your recursive function using DocumentsContract. (In your post, not in comments). Show how you call it/them from onActivityResult(). – blackapps Jun 15 '20 at 10:17
  • @blackapps I don't' think would be useful. I have already shown here the details about the real culprit https://stackoverflow.com/questions/62375696/unexpected-behavior-when-documentfile-fromtreeuri-is-called-on-uri-of-subdirec If you want to avoid too wording in comment, would be enough to show just a working snippet to generate a `DocumentFile` related to a subfolder starting from the `DocumentFile` related to its parent folder in the answer section(if possible), I will accept and upvote the answer, that certainly will be useful to whole community, and everybody will be happy. – AndreaF Jun 15 '20 at 11:13
  • That is not how it goes here. You post your code. You tell the troubles. Then we correct. – blackapps Jun 15 '20 at 11:28
  • @blackapps I have posted the codeline used in the other related question and even the `Uri` returned. Posting a bunch of code that has nothing to do with the problem of the question itself since it's clear the culprit of error and where it happens, it's not needed and makes the question unnecessarily long. (Note I'm member of Stackoverflow since 8 years, I know how it goes here.) – AndreaF Jun 15 '20 at 11:57
  • I am having the same exact issue. Did you ever figure it out? – John Glen Oct 18 '20 at 02:15

0 Answers0