2016-09-29 52 views
0

我正在嘗試獲取包含用戶內部存儲上的MP3文件的所有文件夾的列表。如何加速文件存儲訪問?

這裏是我打電話用於此目的的遞歸函數 -

public void listAllMusicFiles(String pathToDirectory) { 
     String lastFolderPath = ""; 
     int mp3Count = 0; 
     File f = new File(pathToDirectory); 
     File[] files = f.listFiles(); 
     for (File inFile : files) { 
      if (inFile.isDirectory()) { 
       //reset last folder path 
       lastFolderPath = ""; 
       Log.d("Directory ", inFile.getPath()); 
       listAllMusicFiles(inFile.getPath()); 
      } else { 
       if (inFile.getAbsolutePath().endsWith(".mp3") || inFile.getAbsolutePath().endsWith(".MP3")) { 
        mp3Count++; 
        Log.wtf("MP3 Count", mp3Count + " "); 

        //add each folder only once 
        String folderName = inFile.getParentFile().getName(); 
        String folderPath = inFile.getParentFile().getPath(); 
        Log.e("FOUND in", folderPath); 

        //create a new Folder object 
        Folder currentFolder = new Folder(folderName, folderPath, mp3Count + ""); 

        if (!lastFolderPath.equals(folderPath)) { 
         Log.d("NEW", folderPath); 
         lastFolderPath = folderPath; 
         folderArrayList.add(currentFolder); 
        } else { 
         Log.d("OLD", folderPath); 
         //find a Folder object in folderArrayList where the object's path matches current folderPath 
         for (Folder folder : folderArrayList) { 
          String currentPath = folder.getFolder_Path(); 
          if (currentPath.equals(folderPath)) { 
           //found a match 
           //update count 
           folder.setFolder_Song_Count(mp3Count + ""); 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

當我運行我的設備上運行此代碼,我能列出所需文件夾中RecyclerView,但隨着延遲大約6-7秒。

我已經將此任務移至AsyncTask中,以便我的UIThread不會由於此密集操作而掛起。

但是當談到提高文件系統性能時,我完全不知所措。請幫助。謝謝 !

+0

「listAllMusicFiles(字符串pathToDirectory)」。你在Sting和File之間轉換回來和堡壘。使它成爲'listAllMusicFiles(文件目錄)'。 – greenapps

+0

'folderName = inFile.getParentFile()。getName();'那是'f.getName();'。 – greenapps

+0

當一個目錄中有100個mp3文件時,您可以100次迭代數組列表來查看該文件夾是否已經存在。然後你改變那個文件夾的計數一百次。這不是有效的。您應該先計算該文件夾中的文件。當完成該文件夾時,添加名稱並計算一次數組。那麼你不必檢查任何東西。 – greenapps

回答

0

而是在一個ArrayList,並在下一步通過完整列表迭代存儲currentFolder找到該文件夾​​和更新的價值,你可以簡單地使用HashMap中這樣

HashMap<String, Folder> folders = new HashMap<>(); 

    public void listAllMusicFiles(String pathToDirectory) { 

     int mp3Count = 0; 
     File f = new File(pathToDirectory); 
     File[] files = f.listFiles(); 

     Folder folder; 
     String folderName, folderPath; 

     for (File inFile : files) { 
      if (inFile.isDirectory()) { 
       //reset last folder path 
       Log.d("Directory ", inFile.getPath()); 
       listAllMusicFiles(inFile.getPath()); 
      } else { 
       if (inFile.getAbsolutePath().endsWith(".mp3") || inFile.getAbsolutePath().endsWith(".MP3")) { 
        mp3Count++; 
        Log.wtf("MP3 Count", mp3Count + " "); 

        //add each folder only once 
        folderName = inFile.getParentFile().getName(); 
        folderPath = inFile.getParentFile().getPath(); 

        Log.e("FOUND in", folderPath); 

        if (folders.containsKey(folderPath)) { 

         folder = folders.get(folderPath); 
         folder.setFolder_Song_Count(mp3Count + ""); 
         folders.put(folderPath, folder); 
        } else { 

         folder = new Folder(folderName, folderPath, mp3Count + ""); 
         folders.put(folderPath, folder); 
        } 

       } 
      } 
     } 
    } 
+0

最適合多媒體使用MediaStore。。 –