简体   繁体   中英

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

    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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM