Android Glide获取缓存大小与清除缓存

来源:互联网 发布:宝马刷隐藏软件 编辑:程序博客网 时间:2024/05/21 09:11
 

Android Glide获取缓存大小与清除缓存

标签: android缓存应用stringclass
 12634人阅读 评论(18) 收藏 举报
 分类:

目录(?)[+]

GlideCatchSimple

SimpleDemo请看github

https://github.com/YaphetZhao/GlideCatchSimple 
https://github.com/YaphetZhao/GlideCatchSimple/blob/master/apk/app-debug.apk

Glide缓存Simple

  1. 缓存路径的指定
  2. 缓存大小的获取
  3. 磁盘缓存清除(两种方法)
  4. 内存缓存清除

    • 可clone之后查看使用Simple

Glide cache Simple.

  1. The cache path specified
  2. The cache size
  3. The disk cache (two ways)
  4. Memory cache to clearMay

    • use Simple clone after check

GlideCatchUtil

获取Glide磁盘缓存大小

public String getCacheSize() {    try {        return getFormatSize(getFolderSize(new File(Application.getInstance().getCacheDir() + "/" + GlideCatchConfig.GLIDE_CARCH_DIR)));    } catch (Exception e) {        e.printStackTrace();        return "获取失败";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

清除Glide磁盘缓存

public boolean cleanCatchDisk() {    return deleteFolderFile(Application.getInstance().getCacheDir() + "/" + GlideCatchConfig.GLIDE_CARCH_DIR, true);}public boolean clearCacheDiskSelf() {    try {        if (Looper.myLooper() == Looper.getMainLooper()) {            new Thread(new Runnable() {                @Override                public void run() {                    Glide.get(Application.getInstance()).clearDiskCache();                }            }).start();        } else {            Glide.get(Application.getInstance()).clearDiskCache();        }        return true;    } catch (Exception e) {        e.printStackTrace();        return false;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

AndroidMainfest.xml and GlideConfiguration.class

<meta-data    android:name="com.yaphetzhao.glidecatchsimple.glide.GlideConfiguration"    android:value="GlideModule" />
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

Application.class

public class Application extends android.app.Application {    public static Application instance;    public static Application getInstance() {        return instance;    }    @Override    public void onCreate() {        super.onCreate();        instance = this;    }}<application    android:name=".Application"    more...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

About Me

YaphetZhao 
Email:yaphetzhao@gmail.com 
Email_CN:yaphetzhao@foxmail.com 
GitHub:http://github.com/YaphetZhao/ 
QQ:11613371 
CSDN_Blog:http://blog.csdn.net/yaphetzhao

完整工具类代码

package com.yaphetzhao.glidecatchsimple.glide;import android.os.Looper;import android.util.Log;import com.bumptech.glide.Glide;import com.yaphetzhao.glidecatchsimple.Application;import com.yaphetzhao.glidecatchsimple.glide.config.GlideCatchConfig;import java.io.File;import java.math.BigDecimal;/** * Created by YaphetZhao * on 2016/12/19. * <p> * QQ:11613371 * GitHub:https://github.com/YaphetZhao * Email:yaphetzhao@foxmail.com * Email_EN:yaphetzhao@gmail.com * <p> * Glide缓存工具类 */@SuppressWarnings("ResultOfMethodCallIgnored")public class GlideCatchUtil {    private static GlideCatchUtil instance;    public static GlideCatchUtil getInstance() {        if (null == instance) {            instance = new GlideCatchUtil();        }        return instance;    }    // 获取Glide磁盘缓存大小    public String getCacheSize() {        try {            return getFormatSize(getFolderSize(new File(Application.getInstance().getCacheDir() + "/" + GlideCatchConfig.GLIDE_CARCH_DIR)));        } catch (Exception e) {            e.printStackTrace();            return "获取失败";        }    }    // 清除Glide磁盘缓存,自己获取缓存文件夹并删除方法    public boolean cleanCatchDisk() {        return deleteFolderFile(Application.getInstance().getCacheDir() + "/" + GlideCatchConfig.GLIDE_CARCH_DIR, true);    }    // 清除图片磁盘缓存,调用Glide自带方法    public boolean clearCacheDiskSelf() {        try {            if (Looper.myLooper() == Looper.getMainLooper()) {                new Thread(new Runnable() {                    @Override                    public void run() {                        Glide.get(Application.getInstance()).clearDiskCache();                    }                }).start();            } else {                Glide.get(Application.getInstance()).clearDiskCache();            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    // 清除Glide内存缓存    public boolean clearCacheMemory() {        try {            if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行                Glide.get(Application.getInstance()).clearMemory();                return true;            }        } catch (Exception e) {            e.printStackTrace();        }        return false;    }    // 获取指定文件夹内所有文件大小的和    private long getFolderSize(File file) throws Exception {        long size = 0;        try {            File[] fileList = file.listFiles();            for (File aFileList : fileList) {                if (aFileList.isDirectory()) {                    size = size + getFolderSize(aFileList);                } else {                    size = size + aFileList.length();                }            }        } catch (Exception e) {            e.printStackTrace();        }        return size;    }    // 格式化单位    private static String getFormatSize(double size) {        double kiloByte = size / 1024;        if (kiloByte < 1) {            return size + "Byte";        }        double megaByte = kiloByte / 1024;        if (megaByte < 1) {            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";        }        double gigaByte = megaByte / 1024;        if (gigaByte < 1) {            BigDecimal result2 = new BigDecimal(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 = new BigDecimal(teraBytes);        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";    }    // 按目录删除文件夹文件方法    private boolean deleteFolderFile(String filePath, boolean deleteThisPath) {        try {            File file = new File(filePath);            if (file.isDirectory()) {                File files[] = file.listFiles();                for (File file1 : files) {                    deleteFolderFile(file1.getAbsolutePath(), true);                }            }            if (deleteThisPath) {                if (!file.isDirectory()) {                    file.delete();                } else {                    if (file.listFiles().length == 0) {                        file.delete();                    }                }            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }}
  • 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
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 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
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154

完整代码请看git

https://github.com/YaphetZhao/GlideCatchSimple

我的更多git项目

DropDownMultiPager 
仿淘宝商品详情页下拉足迹Demo 
https://github.com/YaphetZhao/DropDownMultiPager

PearGreenDAO 
安卓GreenDao使用例子 Android GreenDAO Simple 
https://github.com/YaphetZhao/PearGreenDAO

  • 欢迎fork,感谢star
原创粉丝点击