How to open my app's external/shared storage directory

137 Views Asked by At

I have a notification displaying progress of downloading a file. Upon completion, tapping on it should open the directory in which the file was saved (my app's external storage directory). The intent is set up in the following way and is later passed to a PendingIntent.

val dir = getExternalFilesDir("storage")
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.data = FileProvider.getUriForFile(context, authority, dir)
intent.type = DocumentsContract.Document.MIME_TYPE_DIR

What actually happens - the file explorer opens, but shows "Recent" directory(?) instead.

What else I tried:

  • passing absolute path instead of content URI, but it makes no difference
  • setting type to "/", it then presents a choice on which app to open the dir with, but the result is ultimately the same - "Recent"
  • using ACTION_OPEN_DOCUMENT intent instead, to see if it also has the same problem, and yes, it also points to "Recent"

Note: FileProvider is set up just fine, the uri passed to the intent's data is correct. Also note: I want to avoid saving those files to "Downloads", if possible.

1

There are 1 best solutions below

6
CommonsWare On

Upon completion, tapping on it should open the directory in which the file was saved (my app's external storage directory).

That has never been a feature of Android.

The intent is set up in the following way and is later passed to a PendingIntent.

FileProvider does not support directories. ACTION_VIEW does not support directories by default, though some file managers might. DocumentsContract has nothing to do with FileProvider.

passing absolute path instead of content URI, but it makes no difference

That should crash your app with a FileUriExposedException.