admin
2022-01-07 8dfe5354073b700af45d5cb472dd5f003e6f3f25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.ysvideo.zhibo.lib.common.util.cache;
 
import android.content.Context;
 
import java.io.File;
import java.text.DecimalFormat;
 
/**
 * 缓存工具类
 */
public class AndroidCacheUtil {
 
    public static long getFileSize(File f) throws Exception// 取得文件夹大小
    {
        long size = 0;
        File flist[] = f.listFiles();
 
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getFileSize(flist[i]);
            } else {
                size = size + flist[i].length();
            }
        }
        return size;
    }
 
    public static String formetFileSize(long fileS) {// 转换文件大小
        if (fileS == 0) {
            return "0KB";
        }
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "K";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "G";
        }
        return fileSizeString;
    }
 
    public static long getCacheSize(Context context) throws Exception {
        return getFileSize(context.getCacheDir())
                + getFileSize(context.getExternalCacheDir());
    }
 
    public static String getCacheSizeDesc(Context context) throws Exception {
        long size = getCacheSize(context);
        return formetFileSize(size);
    }
 
 
    public static boolean trimCache(Context context) {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory() && dir.listFiles().length != 0) {
            return deleteDir(dir);
        } else {
            return false;
        }
    }
 
    public static boolean trimExternalCache(Context context) {
 
        File dir = context.getExternalCacheDir();
        if (dir != null && dir.isDirectory() && dir.listFiles().length != 0) {
            return deleteDir(dir);
        } else {
            return false;
        }
 
    }
 
    /**
     * 清除所有缓存
     * @param context
     * @return
     */
    public static boolean trimAllCache(Context context) {
        try {
            trimCache(context);
            trimExternalCache(context);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
 
    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(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
 
    }
 
}