Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ fun documentFromSingleUri(context: Context, uri: Uri): DocumentFile? {
return DocumentFile.fromSingleUri(context, documentUri)
}



/**
* Generate the `DocumentFile` reference from string `uri`
*/
Expand Down Expand Up @@ -161,12 +163,24 @@ fun traverseDirectoryEntries(
rootOnly: Boolean,
block: (data: Map<String, Any>, isLast: Boolean) -> Unit
): Boolean {
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri,
DocumentsContract.getTreeDocumentId(rootUri)
)

/// Keep track of our directory hierarchy
var childrenUritmp: Uri?;

// https://stackoverflow.com/questions/41096332/issues-traversing-through-directory-hierarchy-with-android-storage-access-framew
// Credit to user: Foobnix
// If we were to always use getTreeDocumentId, it would apparently always only list the top level folder even if you request a subfolder
try {
// for childs and sub child dirs
childrenUritmp = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getDocumentId(rootUri));
} catch (e: Exception) {
// for parent dir
childrenUritmp = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getTreeDocumentId(rootUri));
}
Comment on lines +172 to +178
Copy link
Member

@alexcmgit alexcmgit Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes a lot of sense. I knew something was missing in this function since the subfolders wasn't allowed to be listed even if we had permisions over a parent folder.

Most of times testing and debugging this kind of operation is not trivial and requires a lot of time, thanks for the efforts! I'll make sure to publish into the next release and since it's not a breaking change and fixes a current behavior probably will be done on v0.3.2.


// TODO(@EternityForest, @lakscastro): Remove this variable and use: `val childrenUri = try { ... } catch (e: Exception) { ... }`
val childrenUri = childrenUritmp as Uri;

// Keep track of our directory hierarchy
val dirNodes = mutableListOf<Pair<Uri, Uri>>(Pair(rootUri, childrenUri))

while (dirNodes.isNotEmpty()) {
Expand All @@ -187,7 +201,7 @@ fun traverseDirectoryEntries(
val cursor = contentResolver.query(
children,
projection,
/// TODO: Add support for `selection`, `selectionArgs` and `sortOrder`
// TODO: Add support for `selection`, `selectionArgs` and `sortOrder`
null,
null,
null
Expand Down Expand Up @@ -267,7 +281,7 @@ fun bitmapToBase64(bitmap: Bitmap): String {
}

/**
* Trick to verify if is a tree URi even not in API 26+
* Trick to verify if is a tree URI even not in API 26+
*/
fun isTreeUri(uri: Uri): Boolean {
if (Build.VERSION.SDK_INT >= API_24) {
Expand Down
3 changes: 2 additions & 1 deletion docs/Usage/Storage Access Framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ This method list files lazily **over a granted uri:**
> **Note** `DocumentFileColumn.id` is optional. It is required to fetch the file list from native API. So it is enabled regardless if you include this column or not. And this applies only to this API (`listFiles`).

```dart
/// *Must* be a granted uri from `openDocumentTree`
/// *Must* be a granted uri from `openDocumentTree`, or a URI representing a child under such a granted uri.
final Uri myGrantedUri = ...
final DocumentFile? documentFileOfMyGrantedUri = await myGrantedUri.toDocumentFile();

Expand Down Expand Up @@ -598,6 +598,7 @@ This class represents but is not the mirror of the original [`DocumentFile`](htt

This class is not intended to be instantiated, and it is only used for typing and convenient purposes.


### <samp>QueryMetadata</samp>

This class wraps useful metadata of the source queries returned by the `PartialDocumentFile`.
Expand Down