Home » Java » java – Get list of all files (every file) in both internal and external storage with absolute path in Android-Exceptionshub

java – Get list of all files (every file) in both internal and external storage with absolute path in Android-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m working on an app which analyses the user’s storage and give information like Total Images, Videos, Audios, Archives, Executables and other files. With MediaStore i can only get the Media files files but i want a list of every single file present on the device’s storage (both internal and external) with their absolute path so that i can access them.

How to&Answers:

Try this:

.....
List<File> files = getListFiles(new File("YOUR ROOT")); 
....
private List<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<File>();
    File[] files = parentDir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            inFiles.addAll(getListFiles(file));
        } else {
            if(file.getName().endsWith(".csv")){
                inFiles.add(file);
            }
        }
    }
    return inFiles;
}