I’m working on an Android app using Kotlin, and I need to open the file explorer at a specific directory where a file is located. Specifically, I want to open the “Documents” directory. Additionally, I want to make sure that only file explorer apps are shown as options and not other types of apps.
Here’s the relevant code snippet I’m using:
private val documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
fun open() {
val documentsUri = Uri.fromFile(documents)
val intent = Intent(Intent.ACTION_VIEW).apply {
data = documentsUri
type = "*/*"
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
}
However, I’m encountering a couple of issues:
-
Unrelated apps showing up: when using the code above, on my Android
13 device, I see a lot of unrelated apps in the chooser, such as
Messenger, Signal, Gmail, etc. Only one file explorer app shows up
(although there should be more). -
Opening the wrong folder: when selecting a file explorer, it opens
the “Downloads” folder instead of the “Documents” folder. This might
be due to how the file explorer app handles intents.