【问题标题】:How to optimize External Storage file search on Android如何优化 Android 上的外部存储文件搜索
【发布时间】:2012-09-10 01:07:13
【问题描述】:

要求

扫描外部存储上的所有文件夹和文件以查找所有 apk 文件,并为每个 apk 文件获取信息并显示进度。

到目前为止我是怎么做到的

我最初的想法是,这是一件很容易实现的事情。但是好吧......即使它有点工作,它也没有达到预期的效果。

我使用 AsyncTask 在后台执行扫描。这是我的做法:

 private ArrayList<String> sdcardAppsPath;

  protected class ScanTask extends AsyncTask<Context, Scanner, ArrayList<Apk>> {
     private ArrayList<Apk> sdcardApps;  
    @Override
        protected ArrayList<App> doInBackground(Context... params) {

            visitAllDirsAndFiles(Environment.getExternalStorageDirectory());
            for (String path : sdcardAppsPath) {
                //get info about apk file and
                publishProgress(values)  //show in some textviews number of files scanned, total files
                sdcardApps.add(currentScannedItemFoundInfo);
            }
          return sdcardApps
    }

    @Override
        protected void onProgressUpdate(Scanner... values) {
            super.onProgressUpdate(values);
           // update some textView texts based on values
       }   
    }

还有

  // Process all files and directories under dir
        public void visitAllDirsAndFiles(File dir) {
            if (dir != null) {
                if (dir.getAbsoluteFile().toString().endsWith(".apk")) {
                    sdcardAppsPath.add(dir.getAbsoluteFile().toString());
                }

                if (dir.isDirectory()) {
                    String[] children = dir.list();
                    if (children != null) {
                        for (int i = 0; i < children.length; i++) {
                            visitAllDirsAndFiles(new File(dir, children[i]));
                        }
                    }
                }
            }
        }

正如您在vistitAllDirsAndFiles 之后看到的那样,我得到了一个ArrayList&lt;String&gt;,其中包含我需要的所有apk 文件,因此我可以检查每个我想要的内容。不利的一面是,如果卡上有很多文件并且应用程序在找到路径时没有显示任何进度,则构建此 sdcardAppsPath 可能需要很长时间。

我想要达到的目标

我希望能够在doInBackground 中以某种方式为每个找到的文件,获取我需要的信息,更新进度,然后继续扫描。任何想法如何更新我的代码以像这样工作?

【问题讨论】:

  • 这是完整的代码清单吗?你不需要onProgressUpdate吗?
  • ProgressUpdate 上没有相关部分...
  • 其实,现在我仔细看了一下,如果您想在搜索文件时更新进度,您不应该在visitAllDirsAndFiles 方法中调用publishProgress 吗?

标签: android file path android-sdcard dir


【解决方案1】:

如果您想在搜索文件时更新进度,我认为您可能希望在您的 visitAllDirsAndFiles 方法中调用 publishProgress,而不是之后遍历列表然后调用它...

【讨论】:

    猜你喜欢
    • 2020-10-20
    • 2011-10-23
    • 2012-05-05
    • 2016-10-14
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多