File tree Expand file tree Collapse file tree
android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,6 +43,8 @@ fun documentFromSingleUri(context: Context, uri: Uri): DocumentFile? {
4343 return DocumentFile .fromSingleUri(context, documentUri)
4444}
4545
46+
47+
4648/* *
4749 * Generate the `DocumentFile` reference from string `uri`
4850 */
@@ -161,10 +163,24 @@ fun traverseDirectoryEntries(
161163 rootOnly : Boolean ,
162164 block : (data: Map <String , Any >, isLast: Boolean ) -> Unit
163165): Boolean {
164- val childrenUri = DocumentsContract .buildChildDocumentsUriUsingTree(
165- rootUri,
166- DocumentsContract .getTreeDocumentId(rootUri)
167- )
166+
167+
168+ var childrenUritmp: Uri ? ;
169+
170+
171+ // https://stackoverflow.com/questions/41096332/issues-traversing-through-directory-hierarchy-with-android-storage-access-framew
172+ // Credit to user: Foobnix
173+ // If we were to always use getTreeDocumentId, it would apparently always only list the top level folder even if you request a subfolder
174+
175+ try {
176+ // for childs and sub child dirs
177+ childrenUritmp = DocumentsContract .buildChildDocumentsUriUsingTree(rootUri, DocumentsContract .getDocumentId(rootUri));
178+ } catch (e: Exception ) {
179+ // for parent dir
180+ childrenUritmp = DocumentsContract .buildChildDocumentsUriUsingTree(rootUri, DocumentsContract .getTreeDocumentId(rootUri));
181+ }
182+
183+ val childrenUri = childrenUritmp as Uri ;
168184
169185 // / Keep track of our directory hierarchy
170186 val dirNodes = mutableListOf<Pair <Uri , Uri >>(Pair (rootUri, childrenUri))
You can’t perform that action at this time.
0 commit comments