Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to optimize External Storage file search on Android
    primarykey
    data
    text
    <p><strong>Requirement</strong></p> <p>Scan all folders and files on external storage to find all apk files and for each apk file, get info and display progress. </p> <p><strong>How I did it so far</strong></p> <p>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. </p> <p>I use an AsyncTask to perform the scanning in background. Here is how I do:</p> <pre><code> private ArrayList&lt;String&gt; sdcardAppsPath; protected class ScanTask extends AsyncTask&lt;Context, Scanner, ArrayList&lt;Apk&gt;&gt; { private ArrayList&lt;Apk&gt; sdcardApps; @Override protected ArrayList&lt;App&gt; 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 } } </code></pre> <p>And </p> <pre><code> // 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 &lt; children.length; i++) { visitAllDirsAndFiles(new File(dir, children[i])); } } } } } </code></pre> <p>As you can see after <code>vistitAllDirsAndFiles</code> I get an <code>ArrayList&lt;String&gt;</code> 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 <code>sdcardAppsPath</code> if there are many files on the card and the app shows no progress while the paths are being found.</p> <p><strong>What I want to achieve</strong></p> <p>I want to be able in the <code>doInBackground</code> 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 ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload