计算的缓存大小小于“设置/存储”中显示的大小?

时间:2016-08-10 14:34:16

标签: java android caching

我试图让缓存更清洁。在“清理”之前,我希望用户知道存储了多少缓存。起初我跟着this回答,但我注意到它没有显示“外部存储缓存”。然后我添加了以下代码来计算“外部存储缓存”。

public void SearchForExternalCache(){
    File f= new File(Environment.getExternalStorageDirectory(),"Android/data");
    File ff[]=f.listFiles();
    for (File pf:ff)
        initCache(pf);
}

public static long getExternalStorageStatus(){
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());

    long blockSize = 0;
    long totalSize=0;
    long availableSize=0;
    long freeSize=0;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = statFs.getBlockSizeLong();
        totalSize= statFs.getBlockCountLong()*blockSize;
        availableSize = statFs.getAvailableBlocksLong()*blockSize;
        freeSize = statFs.getFreeBlocksLong()*blockSize;
    } else {
        blockSize = statFs.getBlockSize();
        totalSize= statFs.getBlockCount()*blockSize;
        availableSize = statFs.getAvailableBlocks()*blockSize;
        freeSize = statFs.getFreeBlocks()*blockSize;
    }
    return totalSize-availableSize;
}

public void initCache(File packageFile){
    File cacheFIle=new File(packageFile,"cache");
    if(cacheFIle.exists()){
        E.put(packageFile.getName(),getDirSize(cacheFIle));
        Log.e("Cache Found For",""+packageFile.getName()+" = "+(E.get(packageFile.getName())));
    }
    return;
}

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;
}

然后我修改了onGetStatsCompleted方法来总结总数,但总数仍然比设置/存储中显示的缓存大小小。

任何人都可以解释为什么或如何实现与设置/存储相同的结果?

0 个答案:

没有答案