我想在我的應用程序中計算以MB爲單位的總緩存大小,但它總是給我零。任何人都可以解釋我。請幫我計算緩存大小。 下面我有我的代碼如何計算android programmaticaly中的緩存大小?
private void initializeCache() {
long size = 0;
size += getDirSize(this.getCacheDir());
size += getDirSize(this.getExternalCacheDir());
Toast.makeText(MainActivity.this,""+size,Toast.LENGTH_LONG).show();
}
public long getDirSize(File dir){
long size = 0;
for (File file : dir.listFiles()) {
if (file != null && file.isDirectory()) {
size += getDirSize(file);
} else if (file != null && file.isFile()) {
size += file.length();
}
}
return size;
}
的可能的複製[如何獲得緩存目錄的確切大小:機器人(http://stackoverflow.com/questions/16206722/how-to-get-the-exact-size-of-cache -directory-android) –
但它對我不起作用 –
有沒有你需要的權限? –