我可以从 Storage Access Framework 的 OPEN_DOCUMENT_TREE 成功获取基本路径 Uri。
如何使用为 Android 5.0 (Lollipop) 提供的新 SD 卡访问 API?
private static final int READ_REQUEST_CODE = 42;
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, READ_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code
// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
// response to some other intent, and the code below shouldn't run at all.
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
}
}
}
但是 android 5.0 中有一个错误/功能会破坏本文中引用的递归:
在 Lollipop 上使用 Android 存储访问框架列出文件时出现错误
Uri treeUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
Uri f1 = pickedDir.findFile("MyFolder").getUri();
Log.d(TAG, "f1 = " + f1.toString());
使用 File.listFiles() 返回一个 Null 数组。
我已经知道目标文件夹/文件的完整路径。我想构造一个有效的 DocumentFile Uri,它具有 onActivityResult 中返回的根 Uri 的权限。
我想附加到根 Uri 路径或构建一个与根 Uri 具有相同权限的新 Uri 以访问目标文件夹/文件。