0

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
andras
  • 3,305
  • 5
  • 30
  • 45
  • "I expect the folder to open up" -- I do not know why you would expect that. There has never been a standard protocol for what you are trying to do. – CommonsWare Dec 30 '21 at 14:26
  • So there is no way to open the folder that the user selected earlier? – andras Dec 30 '21 at 14:28
  • If by "open" you mean "view the contents", then no, there is no way to open the folder that the user selected earlier, unless you create your own UI for that. – CommonsWare Dec 30 '21 at 14:38
  • I don't understand the reason why I can't (why can't Android reverse the `content://com.android.externalstorage.documents` into file access or something), but I take your word for it. Thanks! – andras Dec 30 '21 at 14:43
  • 1
    "I don't understand the reason why I can't" -- because Android does not offer the UI that you appear to be seeking. Android does not offer a standard UI for "browse the filesystem". It *does* offer targeted UI for specific actions, such as selecting a document tree (`ACTION_OPEN_DOCUMENT_TREE`, which you are using via `OpenDocumentTree`). And, it does offer standard UI for "view a document" (`ACTION_VIEW`). – CommonsWare Dec 30 '21 at 14:52
  • Have a look at INITIAL_URI. But of course not for ACTION_VIEW. – blackapps Dec 30 '21 at 15:15

0 Answers0