是否有可能很容易地在SD卡上的文件夹的大小?我用一个文件夹的图像缓存,并想present所有缓存图像的总大小。有没有办法来此除了遍历每个文件?它们都位于同一个文件夹内?
Is it possible to easily get the size of a folder on the SD card? I use a folder for caching of images, and would like to present the total size of all cached images. Is there a way to this other than iterating over each file? They all reside inside the same folder?
刚刚经历的所有文件,总结它们的长度:
Just go through all files and sum the length of them:
/**
* Return the size of a directory in bytes
*/
private static long dirSize(File dir) {
if (dir.exists()) {
long result = 0;
File[] fileList = dir.listFiles();
for(int i = 0; i < fileList.length; i++) {
// Recursive call if it's a directory
if(fileList[i].isDirectory()) {
result += dirSize(fileList [i]);
} else {
// Sum the file size in bytes
result += fileList[i].length();
}
}
return result; // return the file size
}
return 0;
}
注:功能手写的,因此无法编译
NOTE: Function written by hand so it could not compile!
编辑:。递归调用固定
编辑:dirList.length改为fileList.length
EDITED: dirList.length changed to fileList.length.