Android之计算缓存大小并且清空缓存

发布时间:2020-07-13 15:21:27 作者:镜中小白
来源:网络 阅读:316

项目中碰到了计算缓存大小和清空缓存的功能,这个很常见的功能,几乎每个APP都有,以为实现很简单,网上搜了一大堆,发现都不是符合我需要的,而且经常删除的没有效果,于是又另外找了一些资料,折腾了蛮久,终于完成了。以下的这个类的功能很简单,计算你的缓存总大小,不管内部缓存还是外部缓存,和清空缓存,包括内部和外部的缓存一起清空:

publicclass DataCleanManager {
    
     public static StringgetTotalCacheSize(Context context) throws Exception {
            long cacheSize =getFolderSize(context.getCacheDir());
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 
                cacheSize +=getFolderSize(context.getExternalCacheDir());
            } 
            return getFormatSize(cacheSize);
        }
  
    public static void clearAllCache(Contextcontext) {
        deleteDir(context.getCacheDir());
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 
           deleteDir(context.getExternalCacheDir());
        } 
    }
  
    private static boolean deleteDir(File dir){
        if (dir != null &&dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i <children.length; i++) {
                boolean success = deleteDir(newFile(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }
      
    // 获取文件 
    //Context.getExternalFilesDir() -->SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据 
    //Context.getExternalCacheDir() -->SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据 
    public static long getFolderSize(File file)throws Exception { 
        long size = 0; 
        try { 
            File[] fileList =file.listFiles(); 
            for (int i = 0; i <fileList.length; i++) { 
                // 如果下面还有文件 
                if (fileList[i].isDirectory()) { 
                    size = size +getFolderSize(fileList[i]); 
                } else { 
                    size = size +fileList[i].length(); 
                } 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return size; 
    } 
      
    /**
     * 格式化单位
     * @param size
     */
    public static String getFormatSize(doublesize) { 
        double kiloByte = size / 1024; 
        if (kiloByte < 1) { 
           return size + "Byte"; 
        } 
  
        double megaByte = kiloByte / 1024; 
        if (megaByte < 1) { 
            BigDecimal result1 = newBigDecimal(Double.toString(kiloByte)); 
            return result1.setScale(2,BigDecimal.ROUND_HALF_UP) 
                    .toPlainString() +"KB"; 
        } 
  
        double gigaByte = megaByte / 1024; 
        if (gigaByte < 1) { 
            BigDecimal result2 = newBigDecimal(Double.toString(megaByte)); 
            return result2.setScale(2,BigDecimal.ROUND_HALF_UP) 
                    .toPlainString() +"MB"; 
        } 
  
        double teraBytes = gigaByte /1024; 
        if (teraBytes < 1) { 
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte)); 
            return result3.setScale(2,BigDecimal.ROUND_HALF_UP) 
                    .toPlainString() +"GB"; 
        } 
        BigDecimal result4 = newBigDecimal(teraBytes); 
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() 
                + "TB"; 
    }  
   
}

当你在项目中需要查下缓存大小,就使用getTotalCacheSize(Context)方法,清空缓存,就使用clearAllCache(Context)方法。当然,开发完APP也是需要进行全方位的检测:www.ineice.com

 


推荐阅读:
  1. SDWebimage清空缓存
  2. UIWebView清空本地缓存

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

安卓 安卓开发 roi

上一篇:Python位运算符

下一篇:ASM实例操作

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》