diff --git a/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt b/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt index 69e2279..0aa9c14 100644 --- a/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt +++ b/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt @@ -240,5 +240,13 @@ abstract class DocumentFileCompat constructor( fun isDocument(context: Context, uri: Uri): Boolean { return isDocumentUri(context, uri) } + + /** + * Return whether the given [uri] is a tree uri. + */ + internal fun isTreeUri(uri: Uri): Boolean { + val paths = uri.pathSegments + return paths.size >= 2 && "tree" == paths[0] + } } } \ No newline at end of file diff --git a/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt b/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt index 1d03cf0..918091a 100644 --- a/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt +++ b/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt @@ -2,6 +2,7 @@ package com.lazygeniouz.dfc.file.internals import android.content.Context import android.net.Uri +import android.provider.DocumentsContract import com.lazygeniouz.dfc.file.DocumentFileCompat import com.lazygeniouz.dfc.resolver.ResolverCompat @@ -96,6 +97,9 @@ internal class SingleDocumentFileCompat( * Build a [SingleDocumentFileCompat] from a given [uri]. */ internal fun make(context: Context, self: Uri): SingleDocumentFileCompat? { + if (isTreeUri(self)) return null + if (!DocumentsContract.isDocumentUri(context, self)) return null + ResolverCompat.getCursor(context, self, ResolverCompat.fullProjection) ?.use { cursor -> if (cursor.moveToFirst()) { diff --git a/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/TreeDocumentFileCompat.kt b/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/TreeDocumentFileCompat.kt index 8fda720..0958c4e 100644 --- a/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/TreeDocumentFileCompat.kt +++ b/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/TreeDocumentFileCompat.kt @@ -103,14 +103,6 @@ internal class TreeDocumentFileCompat constructor( internal companion object { - /** - * Return whether the given [uri] is a tree uri. - */ - private fun isTreeUri(uri: Uri): Boolean { - val paths = uri.pathSegments - return paths.size >= 2 && "tree" == paths[0] - } - /** * Build the initial [TreeDocumentFileCompat] from a given [uri]. */ @@ -120,9 +112,12 @@ internal class TreeDocumentFileCompat constructor( } // build a new tree uri if this is a first tree doc creation... - val treeUri = if (isInitial) DocumentsContract.buildDocumentUriUsingTree( - uri, DocumentsContract.getTreeDocumentId(uri) - ) else uri + // but only if the uri is not already a document uri to preserve subdir info. + val treeUri = if (isInitial && !DocumentsContract.isDocumentUri(context, uri)) { + DocumentsContract.buildDocumentUriUsingTree( + uri, DocumentsContract.getTreeDocumentId(uri) + ) + } else uri ResolverCompat.getCursor(context, treeUri, ResolverCompat.fullProjection) ?.use { cursor ->