如何递归扫描的Andr​​oid目录 [英] How to recursively scan directories in Android

查看:193
本文介绍了如何递归扫描的Andr​​oid目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何递归扫描Android的目录和显示文件名(S)?我试图进行扫描,但它的速度慢(强制关闭或等待)。我使用的是在一个单独的答案的 FileWalker 类=htt​​p://stackoverflow.com/questions/2056221/recursively-list-files-in- Java的>这个问题

How can I recursively scan directories in Android and display file name(s)? I'm trying to scan, but it's slow (force close or wait). I'm using the FileWalker class given in a separate answer to this question.

推荐答案

您应该总是只能从非UI线程访问文件系统。否则,你可能会阻止长时间的UI线程,却得到了ANR。运行FileWalker在的AsyncTask doInBackground()

You should almost always access the file system only from a non-UI thread. Otherwise you risk blocking the UI thread for long periods and getting an ANR. Run the FileWalker in an AsyncTask's doInBackground().

这是FileWalker一个稍微优化版:

This is a slightly optimized version of FileWalker:

public class Filewalker {

    public void walk(File root) {

        File[] list = root.listFiles();

        for (File f : list) {
            if (f.isDirectory()) {
                Log.d("", "Dir: " + f.getAbsoluteFile());
                walk(f);
            }
            else {
                Log.d("", "File: " + f.getAbsoluteFile());
            }
        }
    }

    public static void main(String[] args) {
        Filewalker fw = new Filewalker();
        fw.walk(context.getFilesDir());
    }
}

在一个单独的线程中运行它。

Run it in a separate thread.

这篇关于如何递归扫描的Andr​​oid目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆