0
的數量在我的小文件瀏覽器應用程序,我展示在每個目錄的子元素的數量等:如何高效地獲取子目錄和文件
要設置這些數字,我發動AsyncTask
(所以ListView
將不會從我的Adapter
的getView()
方法和列表中的每個項目得到「laggy」)。但是,在查看某些系統目錄(例如「/」)時,垃圾收集器會變得瘋狂並且性能顯着下降(即使應用程序完成,我的AsyncTask
的多個實例仍然保留在內存中) 。
我敢肯定這是關係到我是如何實現的子目錄和子文件檢查我的AsyncTask
裏面做,但是下面的遞歸方法是我能想到的唯一的事情:
//countHidden is a boolean indicating whether to count hidden files
private int[] getSubFilesCount(File root) {
int fcount = 0;
int dcount = 0;
File[] files = root.listFiles();
if (files != null)
for (File f : files) {
if (f.isDirectory()) {
getSubFilesCount(f);
if (f.isHidden()) {
if (countHidden)
dcount++;
} else {
dcount++;
}
} else {
if (f.isHidden()) {
if (countHidden)
fcount++;
} else {
fcount++;
}
}
}
int[] tcount = { fcount, dcount };
return tcount;
}
問題:有沒有其他辦法來獲得子目錄和文件的數量,那麼上面提到的方法會更快?