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不會由於此密集操作而掛起。
但是當談到提高文件系統性能時,我完全不知所措。請幫助。謝謝 !
「listAllMusicFiles(字符串pathToDirectory)」。你在Sting和File之間轉換回來和堡壘。使它成爲'listAllMusicFiles(文件目錄)'。 – greenapps
'folderName = inFile.getParentFile()。getName();'那是'f.getName();'。 – greenapps
當一個目錄中有100個mp3文件時,您可以100次迭代數組列表來查看該文件夾是否已經存在。然後你改變那個文件夾的計數一百次。這不是有效的。您應該先計算該文件夾中的文件。當完成該文件夾時,添加名稱並計算一次數組。那麼你不必檢查任何東西。 – greenapps