Created
January 6, 2023 14:14
-
-
Save w2sv/6c0d4b7e3d155737ff28ad51c2bc9e29 to your computer and use it in GitHub Desktop.
Get document path corresponding to treeUri on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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