Nana
Nana

Reputation: 1

Get all files in all folders in internal and external storage

I'm working on a project on android studio and I need help.. How can I search for a file by its name in all folders in internal and external storage??

I tried using for loop to search in internal.. And I tested if the file doesn't exist , the path will be the old path+the folder name. It didn't work out

Upvotes: 0

Views: 2109

Answers (1)

Satyam Anand
Satyam Anand

Reputation: 377

    public void getAllDir(File dir) {
        String pdfPattern = ".pdf or .txt or .jpg etc..";

        File listFile[] = dir.listFiles();

        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {

                if (listFile[i].isDirectory()) {
                    getAllDir(listFile[i]);
                } else {
                  if (listFile[i].getName().endsWith(pdfPattern)){
                                      //Do what ever u want

                  }
                }
            }
        }    
    }


and call this function like

getAllDir(Environment.getExternalStorageDirectory());

Upvotes: 1

Related Questions