First off - I haven't had much experience with Java yet, and I'm a beginner when it comes to the Android SDK.
I'm trying to write a music player app, which when started scans the entire SD card for mp3 files.
Currently this is how I'm doing that:
// Recursively add songs in a directory
public void addSongsInDirectory(File dir, String filter) {
File[] files = dir.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
addSongsInDirectory(f, filter);
} else {
// Create new MimetypesFileTypeMap
MimetypesFileTypeMap mtfm = new MimetypesFileTypeMap();
// Add the mp3 mimetype
mtfm.addMimeTypes("audio/mpeg mp3");
// If current song is audio/mpeg, add it to the list
if (mtfm.getContentType(f).equals(filter)) {
songs.add(f.getName());
}
}
}
}
}
Unfortunately, on my handset with many songs on it, this process takes about 30 seconds. How can I optimize this function so that it works faster (and possibly in the background)?