0

Android 5 introduce new API for working with SD-card. If you want to write files into some directory on the SC-card, y need get access to it. As far as I know, it could be done in this way:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);

and then:

public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (resultCode == RESULT_OK) {
        Uri treeUri = resultData.getData();

        getActivity().getContentResolver().takePersistableUriPermission(treeUri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION |
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
     }
}

This code launch folder picker, and when folder is picked it requires permissions on the folder by it's Uri.

But is it possible to get permissions on exact directory by it's path? Without using folder picker. I meant that I know directory, which I want get access, and I don't want to launch Folder Picker, because it will confuse user.

How to open DocumentFile without user interaction?

4

1 回答 1

2

不可能。用户应使用 android 的文件选择器选择目录。访问文件夹的唯一方法是使用:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, 42);

因为文件是由存储访问框架 DocumentsProvider 提供的。实际上它可以从 3-rd 方应用程序返回一些 Uri,这不是真正的文件,而是一些实体,3-rd 方应用程序作为文件存在。

于 2015-05-28T08:07:23.480 回答