Android : Android get all files in directory nullpointerexception

on Sunday, February 1, 2015


I'm trying to get all files of a specific directory on the SD card. Therefore I found this function



public static 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(".mp3")){
inFiles.add(file);
}
}
}

return inFiles;
}


The problem is the following: If the folder is empty, it will throw a NullPointerException. How can I avoid this?


Permissions for read and write on external storage are set.


0 comments:

Post a Comment