Search code examples
androidandroid-fileandroid-external-storage

list up all files with a certain extension on a device


I want to find all the files, in my case pdf's, on a device. Now I wrote this code:

File images = Environment.getExternalStorageDirectory();
        mImagelist = images.listFiles(new FilenameFilter(){
            public boolean accept(File dir, String name)
            {
                return ((name.endsWith(".pdf")));
            }
        });

This code finds only one pdf
/storage/emulated/0/HTC_One_WWE_User_Guide.pdf. I've got several other pdf's in my download folder.

How can I solve this?


Solution

  • Thanks to Juan.

    private void findFiles(String[] paths) {
      for (String path: paths) {
        recursiveScan(new File(path));
      }
    }
    
    private void recursiveScan(File _file) {
      File[] files = _file.listFiles();
      if (files != null && files.length > 0) {
        for (File file: files) {
          if (file.isDirectory()) recursiveScan(file);
          if (file.isFile()) {
            Log.d("Scan: ", file.toString());
          }
        }
      }
    }
    

    You just need to give a File var from where to search.