简体   繁体   中英

How to get all file list in my storage?

I'm making application that find specific file in my storage. So I put all file list into List.

Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));

void searchFile(File directory){
        File[] files = directory.listFiles();

        try{
            if(directory.exists()) {
                File[] files = directory.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if(files[i].exists()) {
                        if (files[i].isDirectory()) {
                            File[] file = files[i].listFiles();
                            for (int j = 0; j < file.length; j++) {
                                searchFile(file[j].getPath());
                            }
                        } else
                            fileList.add(files[i].getPath());
                    }
                }
            }
        } catch(Exception ex){}
    }

But My the number of all file is more than 60000. So When I tried to debug, It worked so slowly.

How can I get All file list in my storage quickly?

Please try the below code.

Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));

void searchFile(File directory){
        try{
            if(directory.exists()) {
                File[] files = directory.listFiles();
                for (File file : files) {
                    if(file.exists()) {
                        if (file.isDirectory()) {
                            File[] innerFiles = file.listFiles();
                            for (File innerFile : innerFiles) {
                                searchFile(innerile.getPath());
                            }
                        } else
                            fileList.add(file.getPath());
                    }
                }
            }
        } catch(Exception ex){}
    }

This will narrow down your performance issue to a certain level.

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