Glide清除缓存(清除内存缓存、本地缓存、清除所有缓存等)

Glide使用讲解

https://blog.csdn.net/bzlj2912009596/article/details/81702367 

 Glide的图片缓存在/data/data/项目包名/cache/image_manager_disk_cache文件夹下

public class GlideCacheUtil {

    private static GlideCacheUtil instance;

    public static GlideCacheUtil getInstance(){
        if(instance == null){
            instance = new GlideCacheUtil();
        }
        return instance;
    }

    /**
     * 清理图片磁盘缓存
     */
    public void clearImageDiskCache(final Context context){
        try{
            if(Looper.myLooper() == Looper.getMainLooper()){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(context).clearDiskCache();
                    }
                }).start();
            }else{
                Glide.get(context).clearDiskCache();
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 清除图片内存缓存
     */
    public void clearImageMemoryCache(final Context context){
        try{
            if(Looper.myLooper() == Looper.getMainLooper()){
                Glide.get(context).clearMemory();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 删除指定目录下的文件,这里用于缓存的删除
     *
     * @param filePath filePath
     * @param deleteThisPath deleteThisPath
     */
    private void deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            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();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 清除图片所有缓存
     * 主要调用这个方法
     */
    public void clearImageAllCache(Context context) {
        clearImageDiskCache(context);
        clearImageMemoryCache(context);
        String ImageExternalCatchDir=context.getExternalCacheDir()+ ExternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;
        deleteFolderFile(ImageExternalCatchDir, true);
    }
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值