3

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. Thank you.

Mervin Hemaraju
  • 1,376
  • 1
  • 16
  • 51

1 Answers1

7

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.

CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367