Search code examples
androidkotlinandroid-fileandroid-storage

Android: Folder Picker ONLY


I have an application where when I press on a button it should open the folder picker and allow the user to browse and select a folder ONLY. Then I want to get the folder's path so that I can do some manipulation with it.

I've tried using this code:

val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "*/*"
startActivityForResult(intent, 8778)

But it doesn't work.

Can you please help by providing me with some code?


Solution

  • it should open the folder picker and allow the user to browse and select a folder ONLY

    The closest thing that Android has to something like that is ACTION_OPEN_DOCUMENT_TREE. This allows the user to pick a document tree, which could be a directory on the filesystem, something offered by a cloud storage provider, or other document tree structures.

    Then i want to get the folder's path so that i can do some manipulation with it.

    If by "path" you mean "filesystem path", you have two problems:

    1. A document tree is not necessarily a directory on the filesystem, and you have no reliable means of getting a filesystem path for one

    2. You have no access to arbitrary filesystem locations on Android Q (by default) and Android R+ (for all apps)

    You may want to spend some time learning about the Storage Access Framework in general and ACTION_OPEN_DOCUMENT_TREE in particular.