I have a Uri
, that looks like this:
// directoryBase value is:
// content://com.android.externalstorage.documents/tree/home%3Amyfolder`
val directoryBase = ...
I got it using this code:
rememberLauncherForActivityResult(
contract = ActivityResultContracts.OpenDocumentTree(),
onResult = { uri ->
if (uri != null) {
viewModel.setDirectory(uri)
}
},
)
I can convert this Uri
into a document reference (this form is required for DocumentsContract.createDocument
), using this code:
val id = DocumentsContract.getTreeDocumentId(directoryBase)
// directory is:
// content://com.android.externalstorage.documents/tree/home%3Amyfolder/document/home%3Amyfolder
val directory = DocumentsContract.buildDocumentUriUsingTree(directoryBase, id)
The way I am constructing the Intent
for opening the directory is by using this code:
Intent(Intent.ACTION_VIEW)
.apply {
// neither `directory` nor `directoryBase` works, see explanation below
setDataAndType(directory, "application/*")
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
.run {
PendingIntent.getActivity(
context.applicationContext,
0,
this,
PendingIntent.FLAG_IMMUTABLE,
)
}
What happens?
App selector opens up. Samsung File
just opens up in the root directory, Google Files
crashes. To sum up, I expect the folder to open up, but different file managers fail to do it in different ways.
Please let me also explain why I don't think is a duplicate question:
- this question uses
file://
uri - same goes for this question, not to mention that the accepted answer requires
ES file explorer
- this question and its answer is the same, but it seems that their
content:
uri is different and it does not work anymore with Storage Access Framework