I am trying to calculate total cache size in MB in my app but its giving me Zero all time. Can anyone explain me.Please help me to calculate cache size. below i have my code
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;
}