Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Android runs on a lot of hardware, much of which may not have much stack at all; instead of recursing through the subdirectories, do a breadth-first search, thus:</p> <pre><code>private List&lt;File&gt; getFileListingNoSort(File aStartingDir) { // assuming aStartingDir is a valid input List&lt;File&gt; dirsToSearch = new ArrayList&lt;File&gt;(); dirsToSearch.add(aStartingDir); List&lt;File&gt; resultArray = new ArrayList&lt;File&gt;(); do{ File thisDir = dirsToSearch.remove(0); List&lt;File&gt; filesDirs = Arrays.asList(thisDir.listFiles()); for (File file : filesDirs) { if (file.isDirectory()) { dirsToSearch.add(file); } else if( file.canRead() &amp;&amp; !file.isHidden() &amp;&amp; !file.getName().toLowerCase(loc).startsWith(CACHE) &amp;&amp; !file.getName().toLowerCase(loc).startsWith(TEMP)) { resultArray.add(file); } } } while(false == dirsToSearch.isEmpty()); return resultArray; } </code></pre> <p>caveat emptor: I didn't run or even see if this code compiles.</p> <p>But the idea is, maintain a list of directories, starting with the directory you are care about, remove the first directory from that list, add the files from that directory to the results (modify the code to add the directories to the resultArray if you want directories too), add the directories to the list of directories to search, and continue until the list of directories is empty.</p> <p>Recursion is bad if you can't know in advance how far you have to recurse, or how far you are able. I don't believe that file-system iteration is an appropriate place for recursion, but that's my own opinion.</p>
    singulars
    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.
    1. VO
      singulars
      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