相关疑难解决方法(0)

使用 MTP 通过 Android Storage Access Framework/DocumentProvider 遍历目录层次结构的问题

更新:我最初的问题可能具有误导性,所以我想重新表述它:我想通过 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);
}
Run Code Online (Sandbox Code Playgroud)

然后 Android SAF 选择器启动,我看到我连接的设备被识别为 MTP 数据源。我选择了上述数据源,然后Uri从我的onActivityResult

@Override
public void onActivityResult(int requestCode, …
Run Code Online (Sandbox Code Playgroud)

android mtp android-external-storage storage-access-framework document-provider

7
推荐指数
1
解决办法
2455
查看次数

如何有效地列出包括子目录在内的目录中的所有文件?

我正在开发一个画廊应用程序,该应用程序显示手机或笔式驱动器中的所有图像。我成功地列出了所有图像并将其显示在应用程序中。但我认为它很慢。我使用的Depth First Search技术内的AsyncTask。那么有没有其他方法可以在里面使用AsyncTask,速度要快得多。这里的 root 是一个由树 URI 组成的 DocumentFile。

这是我使用过的代码。

public class ImageBackgroundTask extends AsyncTask<Object, Object, ArrayList<DocumentFile>> {
DocumentFile root;
ArrayList<DocumentFile> result;
ProgressDialog pg;
Context context;
private AsyncTaskCompleteListener<ArrayList<DocumentFile> > callback;

ImageBackgroundTask(DocumentFile root, Context context, AsyncTaskCompleteListener<ArrayList<DocumentFile>> cb){
    this.context=context;
    this.root=root;
    this.callback = cb;

}
@Override
protected ArrayList<DocumentFile> doInBackground(Object... voids) {
    Queue<DocumentFile> stack=new ArrayDeque<>();
    ArrayList<DocumentFile> list=new ArrayList<>();
    for(DocumentFile f:root.listFiles()){
        stack.add(f);
    }
    while(!stack.isEmpty()){
        DocumentFile child=stack.remove();
        if(child.isDirectory()){
            for(DocumentFile file:child.listFiles()){
                stack.add(file);
            }
        }
        else if(child.isFile()){
            String name=child.getName();
            if(name.endsWith(".jpg")
                    || name.endsWith(".png")
                    || …
Run Code Online (Sandbox Code Playgroud)

file-io android gallery image-gallery android-asynctask

0
推荐指数
1
解决办法
1763
查看次数