[Android] Speed up the SD scan

Jean-Baptiste Kempf git at videolan.org
Tue Aug 14 14:21:50 CEST 2012


android | branch: master | Jean-Baptiste Kempf <jb at videolan.org> | Tue Aug 14 14:15:56 2012 +0200| [23b715d8d2b82ccd9f3881acae8e00042c0c5083] | committer: Jean-Baptiste Kempf

Speed up the SD scan

Close #7089

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=23b715d8d2b82ccd9f3881acae8e00042c0c5083
---

 vlc-android/src/org/videolan/vlc/MediaLibrary.java |   68 ++++++++------------
 1 file changed, 27 insertions(+), 41 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
index 4e4ae99..c702943 100644
--- a/vlc-android/src/org/videolan/vlc/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
@@ -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));
                 }
             }
 



More information about the Android mailing list