I have the following code which scan a device for directories which contain mp3.
The following does the job, but unfortunately in some devices where the amount of directories is huge, it takes too long to scan.
Is there a way to speed it up?
scanDirectory(Environment.getExternalStorageDirectory());
private void scanDirectory(File dir) {
File[] files = dir.listFiles();
if( files != null ){
for( int i=0; i<files.length; i++ ){
File file = files[i];
if( file.isDirectory() ){
if( hasAudioFile(file) ){
mFolders.add(file.getAbsolutePath()); // this directory has a mp3 file
}
scanDirectory(file);
}
}
}
}
private boolean hasAudioFile(File dir){
File[] files = dir.listFiles();
if( files == null ){
return false;
}
for( int i=0; i<files.length; i++ ){
File file = files[i];
if( !file.isDirectory() ){
if( file.getName().toLowerCase().endsWith(".mp3") ){
return true;
}
}
}
return false;
}
I know that there is a method to scan all music files in a device which more faster(like this one), but in my case I want the folders not the files.