0

Requirement

Scan all folders and files on external storage to find all apk files and for each apk file, get info and display progress.

How I did it so far

My initial thought is that this is a simple thing to achieve. But well... even it it sort of works, it does not perform as expected.

I use an AsyncTask to perform the scanning in background. Here is how I do:

 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
       }   
    }

And

  // 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]));
                        }
                    }
                }
            }
        }

As you can see after vistitAllDirsAndFiles I get an ArrayList<String> with all apk files I need, so I can check for each what I want. The downside is that it might take a lot of time to build this sdcardAppsPath if there are many files on the card and the app shows no progress while the paths are being found.

What I want to achieve

I want to be able in the doInBackground to somehow for each found file, get the info I need, update progress and then continue with scanning. Any ideas how to update my code to work like this ?

Alin
  • 14,809
  • 40
  • 129
  • 218

1 Answers1

0

I would think that you would want to call publishProgress inside your visitAllDirsAndFiles method if you want to update progress while searching for files, instead of iterating through the list afterwards and calling it then...

Donald Smith
  • 150
  • 1
  • 8