小编Viv*_*tel的帖子

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

我正在开发一个画廊应用程序,该应用程序显示手机或笔式驱动器中的所有图像。我成功地列出了所有图像并将其显示在应用程序中。但我认为它很慢。我使用的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
查看次数