title: Most efficient way to read all files into a list in Android
body: <p>My Android application needs to look into the default Camera directory and get the names of JPEG files created within a time range. I currently do this with the below code snippet.
  The idea is to filter out files not only by the "jpg" extension but also to use the fact that the file names have date/time in them.</p>
  
  <p>I run the below getImages() function within a Thread so the UI itself isn't affected.
  I notice that it takes a while if there are lot of files in the directory even after all this. Is there a faster algorithm or Android specific functions that I can use to optimize it further?</p>
  
  <pre><code>
  private boolean getImages()
      {
          boolean status=true;
          File dir = new File(picPath);
          //Optimize file search by setting the common file prefix as a filter
          //get the date format used by camera to store files.
          String startStr=startTime.format3339(false);
          String endStr=endTime.format3339(false);
          //Loop through each character of start time 
          //and compare with end time till a mismatch is found
          int len=startStr.length();
          int idx=0;
          for ( idx=0;id &lt; len;idx++ )
          {       
              if (startStr.charAt(idx) != endStr.charAt(idx))
              {
                  Log.d(TAG,"breaking at idx "+ idx);
                  break;
              }
          }
          filterString=endStr.substring(0,idx).replace("T", " ").replace(":",".");
          Log.d(TAG,"Filter String" + filterString);
  
          String[] children = dir.list(new JpegFilter());
          List matchedFiles = new ArrayList();
          Log.d(TAG,dir.getAbsolutePath()+" has "+ children.length+ " files");
          int numfiles = children.length;
          for(int i=0;i &lt; numfiles;i++)
          {
              //Get file modify time
              File file = new File(picPath+File.separator+children[i]);
              // Get the last modification information.
              long lastModified = file.lastModified();
              if (lastModified &gt; =startTime.toMillis(false)) //If file modified date greater than equals start time
              {
                  Log.d(TAG,"Match! " + children[i]);
                  if (lastModified &lt; =endTime.toMillis(false))//If file modified date less than equals end time
                  {
                      matchedFiles.add(picPath+File.separator+children[i]);
                  }
                  else //We are now over the time range, quit loop to save time 
                  {
                      break; //TODO break out of for loop
                  }
              }
          }
          if (!matchedFiles.isEmpty()) //If there are images to be processed
          {
              status=convertToPDF(matchedFiles,pdfFile);
              Log.d(TAG,"convertToPDF() returned " + status); 
          }
  
          return status;
      }
  
  </code></pre>
  
  <p>This is the Filter class I use</p>
  
  <pre><code>
  class JpegFilter implements FilenameFilter {
      public boolean accept(File dir, String name) {
          return (name.startsWith(captureJPEG.filterString) && name.endsWith(".jpg"));
      }
  }
  </code></pre>