You have to recursively call sd card root folder and iterate through each folder and extract the image files add the file to an array list or array.Try below code.
public static ArrayList<File> getFolderList(File f, ArrayList<File> dir) {
if (f == null) {
return dir;
} else
try {
if (f.isDirectory()) {
File[] listFile = f.listFiles();
if (listFile != null) {
fileCount = listFile.length;
for (int i = 0; i < fileCount; i++) {
if (listFile[i].isDirectory()) {
dir = getFolderList(listFile[i], dir);
} else {
if (fName.endsWith(".png")
|| fName.endsWith(".jpeg")
|| fName.endsWith(".jpg")
|| fName.endsWith(".bmp")) {
dir.add(listFile[i]);
}
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dir;
}
Parameters :
File f : pass f as sdcard root folder path or any folder path you want to get the count
ArrayList dir : pass an empty arraylist to get the file list. If you want the file count only, then change the return type of the function and also pass any parameter that counts the number of file.