android - 在android中创建一个快速文件搜索功能

标签 android android-asynctask file-search

我是 android 的新手,我正在尝试开发包含搜索功能的文件浏览器。我正在使用递归搜索功能,该功能在具有几个子文件夹和文件的文件夹中运行良好,但由于某种原因它非常慢并且可以在具有大量子文件夹和文件的文件夹中“强制关闭”,因为内存不足。我通过创建将放置结果的 ArrayList 来进行搜索,然后调用将填充列表的递归函数。 “path”参数是搜索的起始文件,“query”是搜索查询。

ArrayList<File> result = new ArrayList<File>();
fileSearch(path, query, result);

这是递归函数的样子:

private void fileSearch(File dir, String query, ArrayList<File> res) {
    if (dir.getName().toLowerCase().contains(query.toLowerCase()))
        res.add(dir);
    if (dir.isDirectory() && !dir.isHidden()) {
        if (dir.list() != null) {
            for (File item : dir.listFiles()) {
                fileSearch(item, query, res);
            }
        }
    }
}

如果有人能指出一种执行更快和/或更高效文件搜索的方法,我将不胜感激。

编辑:

这就是我尝试使用 AsyncTask 完成这项工作的方式:

private class Search extends AsyncTask<File, Integer, Void> {

    String query;
    ArrayList<File> result = new ArrayList<File>();

    public Search(String query){
        this.query = query;
        setTitle("Searching");
    }

    @Override
    protected Void doInBackground(File... item) {
        int count = item.length;
        for (int i = 0; i < count; i++) {
            fileSearch(item[i], query, result);
            publishProgress((int) ((i / (float) count) * 100));
            // Escape early if cancel() is called
            if (isCancelled()) break;
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        setProgress(progress[0]);
    }

    protected void onPostExecute() {
        searchResults = new ListItemDetails[result.size()];
        for (int i = 0; i < result.size(); i++) {
            File temp = result.get(i);
            if (temp.isDirectory())
                searchResults[i] = new ListItemDetails(temp.getAbsolutePath(),
                        R.drawable.folder, temp.lastModified(), temp.length());
            else {
                String ext;
                if (temp.getName().lastIndexOf('.') == -1)
                    ext = "";
                else
                    ext = temp.getName().substring(
                            temp.getName().lastIndexOf('.'));
                searchResults[i] = new ListItemDetails(temp.getAbsolutePath(),
                        getIcon(ext), temp.lastModified(), temp.length());
            }
        }
        finishSearch();
    }

}

public void finishSearch() {
    Intent intent = new Intent(this, SearchResults.class);
    startActivity(intent);
}

调用 finishSearch() 只是为了让我可以创建 Intent 以在其他 Activity 中显示结果。任何想法,建议,提示?提前致谢

最佳答案

您可能正在点击 symbolic links并使用您的搜索功能进入无限循环并耗尽您的应用程序的可用内存。

我建议您保留一个单独的列表,其中包含您访问过的目录的规范路径 ( File.getCanonicalPath() ),并避免一遍又一遍地访问它们。

关于android - 在android中创建一个快速文件搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916600/

相关文章:

android - 如何更新库 ( .aar ) 文件中的谷歌播放服务版本

android - 在操作栏的 searchView 上进行实时搜索

android - 自定义异步任务的实现不起作用

android - Android 登录中的 Soap Web 服务

java - 有没有办法在代码中编写 Eclipse 文件搜索(Ctrl + H)?

python - 在python中搜索具有相同文件名的所有文件的最有效方法

java - 有什么办法可以显着提高这个文件搜索的效率吗?

android - 使用 Intent.ACTION_SEND 和 Intent.EXTRA_STREAM 与 Google+ 应用共享图片

java - 如何从 AsyncTask 更新 View ?

Android INJECT_EVENTS 权限