Commit 23b715d8 authored by Jean-Baptiste Kempf's avatar Jean-Baptiste Kempf

Speed up the SD scan

Close #7089
parent 7be16e47
......@@ -193,63 +193,49 @@ public class MediaLibrary {
int count = 0;
int total = 0;
//first pass : count total files
ArrayList<File> mediaToScan = new ArrayList<File>();
// Count total files, and stack them
while (!directories.isEmpty()) {
File dir = directories.pop();
File[] f = null;
if (new File(dir.getAbsolutePath() + "/.nomedia").exists()) {
continue;
}
if ((f = dir.listFiles(mediaFileFilter)) != null) {
for (int i = 0; i < f.length; i++) {
File file = f[i];
if (file.isFile()) {
total++;
mediaToScan.add(file);
} else if (file.isDirectory()) {
directories.push(file);
}
}
}
}
directories.addAll(mDBManager.getMediaDirs());
if (directories.isEmpty())
directories.add(new File(root));
//second pass : load Medias
while (!directories.isEmpty()) {
File dir = directories.pop();
File[] f = null;
/* .nomedia tells media players on Android to skip the
* folder in the media library because they don't contain
* useful music, such as notification sounds,
* navigation voice phrases etc.
*/
if(new File(dir.getAbsolutePath() + "/.nomedia").exists()) {
continue;
}
if ((f = dir.listFiles(mediaFileFilter)) != null) {
for (int i = 0; i < f.length; i++) {
File file = f[i];
if (file.isFile()) {
MainActivity.sendTextInfo(mContext, file.getName(), count, total);
count++;
String fileURI = Util.PathToURI(file.getPath());
if (existingMedias.containsKey(fileURI)) {
/** only add file if it is not already in the
* list. eg. if user select an subfolder as well
*/
if (!addedLocations.contains(fileURI)) {
// get existing media item from database
mItemList.add(existingMedias.get(fileURI));
addedLocations.add(fileURI);
}
} else {
// create new media item
mItemList.add(new Media(fileURI, true));
}
} else if (file.isDirectory()) {
directories.push(file);
}
// Process the stacked items
for (File file : mediaToScan) {
String fileURI = Util.PathToURI(file.getPath());
MainActivity.sendTextInfo(mContext, file.getName(), count,
total);
count++;
if (existingMedias.containsKey(fileURI)) {
/**
* only add file if it is not already in the list. eg. if
* user select an subfolder as well
*/
if (!addedLocations.contains(fileURI)) {
// get existing media item from database
mItemList.add(existingMedias.get(fileURI));
addedLocations.add(fileURI);
}
} else {
// create new media item
mItemList.add(new Media(fileURI, true));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment