【发布时间】:2017-04-27 01:15:01
【问题描述】:
更新:
我最初的问题可能具有误导性,所以我想改写一下:
我想通过 Android 的存储访问框架从 MTP 连接的设备遍历层次结构树。我似乎无法做到这一点,因为我得到一个SecurityException 说明子节点不是其父节点的后代。有没有办法解决这个问题?或者这是一个已知问题?谢谢。
我正在编写一个 Android 应用程序,它尝试使用 Android 的存储访问框架 (SAF) 通过 MtpDocumentsProvider 遍历和访问层次结构树中的文档。我或多或少遵循https://github.com/googlesamples/android-DirectorySelection 中描述的代码示例,了解如何从我的应用程序启动 SAF Picker,选择 MTP 数据源,然后在onActivityResult 中,使用返回的Uri 遍历层次结构.不幸的是,这似乎不起作用,因为一旦我访问一个子文件夹并尝试遍历它,我总是会得到一个 SecurityException 说明 document xx is not a descendant of yy
所以我的问题是,使用MtpDocumentProvider,我怎样才能成功地从我的应用程序遍历层次结构树并避免这个异常?
具体来说,在我的应用程序中,我首先调用以下方法来启动 SAF Picker:
private void launchStoragePicker() {
Intent browseIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
browseIntent.addFlags(
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
| Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
);
startActivityForResult(browseIntent, REQUEST_CODE_OPEN_DIRECTORY);
}
Android SAF 选择器随后启动,我看到我连接的设备被识别为 MTP 数据源。我选择了上述数据源,然后从我的onActivityResult 中获得了Uri:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_OPEN_DIRECTORY && resultCode == Activity.RESULT_OK) {
traverseDirectoryEntries(data.getData()); // getData() returns the root uri node
}
}
然后,使用返回的Uri,我调用DocumentsContract.buildChildDocumentsUriUsingTree 以获取Uri,然后我可以使用它来查询和访问树层次结构:
void traverseDirectoryEntries(Uri rootUri) {
ContentResolver contentResolver = getActivity().getContentResolver();
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getTreeDocumentId(rootUri));
// Keep track of our directory hierarchy
List<Uri> dirNodes = new LinkedList<>();
dirNodes.add(childrenUri);
while(!dirNodes.isEmpty()) {
childrenUri = dirNodes.remove(0); // get the item from top
Log.d(TAG, "node uri: ", childrenUri);
Cursor c = contentResolver.query(childrenUri, new String[]{Document.COLUMN_DOCUMENT_ID, Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
try {
while (c.moveToNext()) {
final String docId = c.getString(0);
final String name = c.getString(1);
final String mime = c.getString(2);
Log.d(TAG, "docId: " + id + ", name: " + name + ", mime: " + mime);
if(isDirectory(mime)) {
final Uri newNode = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, docId);
dirNodes.add(newNode);
}
}
} finally {
closeQuietly(c);
}
}
}
// Util method to check if the mime type is a directory
private static boolean isDirectory(String mimeType) {
return DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType);
}
// Util method to close a closeable
private static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException re) {
throw re;
} catch (Exception ignore) {
// ignore exception
}
}
}
外部 while 循环的第一次迭代成功:对 query 的调用返回了一个有效的 Cursor 供我遍历。问题是第二次迭代:当我尝试查询 Uri(恰好是 rootUri 的子节点)时,我得到一个 SecurityException 说明文档 xx 不是 yy 的后代。
D/MyApp(19241): 节点 uri: content://com.android.mtp.documents/tree/2/document/2/children D/MyApp(19241):docId:4,名称:DCIM,mime:vnd.android.document/directory D/MyApp(19241): 节点 uri: content://com.android.mtp.documents/tree/2/document/4/children E/DatabaseUtils(20944):将异常写入包裹 E/DatabaseUtils(20944): java.lang.SecurityException: 文档 4 不是 2 的后代
谁能提供一些关于我做错了什么的见解?如果我使用不同的数据源提供程序,例如来自外部存储的数据源提供程序(即通过标准 USB OTG 读卡器连接的 SD 卡),一切正常。
附加信息:
我在 Nexus 6P、Android 7.1.1 上运行它,我的应用 minSdkVersion 是 19。
【问题讨论】:
-
and I see my connected device recognized as the MTP data source.。你能详细说明一下吗?中期计划?您是否在 Android 设备上连接了设备?如何?什么样的设备? -
if(isDirectory(mime))。您没有发布该功能的代码。你也没有解释。 -
你有 Uri rootUri 和 Uri uri。但后者没有解释。
-
closeQuietly(childCursor);childCursor? -
@greenapps 嗨,是的,我使用 USB-C 到 USB-C 电缆在我的 Android 手机上连接了相机设备。 Android 成功检测到设备并确定我可以通过 MTP 访问内容。
isDirectory和closeQuietly方法只是辅助方法。我在我编辑的帖子中添加了代码。rootUri是从onActivityResult返回的Uri。我犯了一个复制/粘贴错误,我已经修复了。
标签: android android-external-storage mtp storage-access-framework document-provider