简体   繁体   English

如何在Android Programmaticaly中计算缓存大小?

[英]How to calculate cache size in android programmaticaly?

I am trying to calculate total cache size in MB in my app but its giving me Zero all time. 我正在尝试计算应用程序中的总缓存大小(以MB为单位),但始终为零。 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;
}

Give this permissions in the AndroidMenifest.xml file. 在AndroidMenifest.xml文件中授予此权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM