0

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;
}
Usman Ahmed
  • 39
  • 3
  • 9

1 Answers1

0

Give this permissions in the AndroidMenifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80