Android Glide獲取緩存大小與清除緩存

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 "獲取失敗";
    }
}

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

AndroidMainfest.xml and GlideConfiguration.class

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

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...

About Me

YaphetZhao
Email:[email protected]
Email_CN:[email protected]
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:[email protected]
 * Email_EN:[email protected]
 * <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;
        }
    }

}

完整代碼請看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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章