title: Why is DocumentFile so slow, and what should I use instead?
body: <p>I want to create something like "PDF Viewer app". Application will search for all *.pdf files in location chosen by user. User can choose this folder by this function:</p>
  
  <pre><code>Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
  startActivityForResult(intent, REQUEST_CODE);
  </code></pre>
  
  <p>Then I get DocumentFile (folder):</p>
  
  <pre><code>@Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == getActivity().RESULT_OK &amp;&amp; requestCode == REQUEST_CODE) {
          Uri uriTree = data.getData();
          DocumentFile documentFile = DocumentFile.fromTreeUri(getActivity(), uriTree);
          //rest of code here
      }
  }
  </code></pre>
  
  <p>Why I chose this method of selecting folder? Because I want to make possible to choose Secondary Storage (you know, in Android >= 5.0, you can't access Secondary Storage with Java.io.file).</p>
  
  <p>Ok, so I get folder with all *.pdf as DocumentFile. Then I call:</p>
  
  <pre><code>for(DocumentFile file: documentFile.listFiles()){
      String fileNameToDisplay = file.getName();
  }
  </code></pre>
  
  <p>And this is <strong>VERY SLOW</strong>. It takes almost 30 seconds when there are ~600 files in chosen folder. To prove it, I chose directory from External Storage (not secondary storage), and then I tried two solutions: DocumentFile and File.
  File version looks like it:</p>
  
  <pre><code>File f = new File(Environment.getExternalStorageDirectory()+"/pdffiles");
      for(File file: f.listFiles()){
          String fileNameToDisplay = file.getName();
      }
  }
  </code></pre>
  
  <p>Second version works about 500x faster. There is almost no time in displaying all files on List View.</p>
  
  <p>Why is <strong>DocumentFile</strong> so slow?</p>