Android:仅文件夹选择器

Android: Folder Picker ONLY

我有一个应用程序,当我按下一个按钮时,它应该打开文件夹选择器并允许用户浏览和 select 一个文件夹。然后我想获取文件夹的路径,以便我可以对其进行一些操作。

我试过使用此代码:

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

但是不行。

你能帮我提供一些代码吗?谢谢。

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

与 Android 最接近的是 ACTION_OPEN_DOCUMENT_TREE。这允许用户选择文档树,它可以是文件系统上的目录、云存储提供商提供的内容或其他文档树结构。

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

如果 "path" 你的意思是 "filesystem path",你有两个问题:

  1. 文档树不一定是文件系统上的目录,并且您没有可靠的方法来获取一个文件系统路径

  2. 您无权访问 Android Q(默认)和 Android R+(对于所有应用程序)上的任意文件系统位置

您可能想花一些时间了解 the Storage Access Framework in general and ACTION_OPEN_DOCUMENT_TREE in particular