Skip to content

Instantly share code, notes, and snippets.

@w2sv
Created January 6, 2023 14:14
Show Gist options
  • Save w2sv/6c0d4b7e3d155737ff28ad51c2bc9e29 to your computer and use it in GitHub Desktop.
Save w2sv/6c0d4b7e3d155737ff28ad51c2bc9e29 to your computer and use it in GitHub Desktop.
Get document path corresponding to treeUri on Android
/**
* Way 1
*/
fun treeUriPath(contentResolver: ContentResolver, treeUri: Uri): DocumentsContract.Path? =
DocumentsContract.findDocumentPath(
contentResolver,
DocumentsContract.buildChildDocumentsUriUsingTree(
treeUri,
DocumentsContract.getTreeDocumentId(treeUri)
)
)
/**
* Way 2, seemingly yielding the same results as Way 1 according to observation
*/
fun treeUriPath(treeUri: Uri): String {
val documentUri = DocumentsContract.buildDocumentUriUsingTree(
treeUri,
DocumentsContract.getTreeDocumentId(treeUri)
)
return documentUri.pathSegments[1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment