清除Glide緩存
Glide自帶清除緩存的功能,分別對應(yīng)Glide.get(context).clearDiskCache();(清除磁盤緩存)與Glide.get(context).clearMemory();(清除內(nèi)存緩存)兩個方法.其中clearDiskCache()方法必須運行在子線程,clearMemory()方法必須運行在主線程,這是這兩個方法所強(qiáng)制要求的,詳見源碼.
獲取Glide緩存空間大小
這個網(wǎng)上也有過一些介紹,但是給出的實現(xiàn)代碼存在一些問題,我這里做了一定的修改.一下方法適合在Glide為默認(rèn)的緩存目錄的情況,不論是內(nèi)部存儲空間還是外部.因為我們可以通過InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR與ExternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR獲取到位于內(nèi)部與外部存儲的緩存文件夾的文件夾名,并通過context.getCacheDir()與context.getExternalCacheDir()獲取內(nèi)部與外部存儲的路徑.進(jìn)而可以通過遍歷文件夾內(nèi)的文件進(jìn)行緩存文件大小求和與全部清除.以下工具類在其他的文章中有前輩寫過,但是存在一些已知的問題,這里做了一些修改.
import android.content.Context;import android.os.Looper;import android.text.TextUtils;import com.bumptech.glide.Glide;import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;import java.io.File;import java.math.BigDecimal;/**Glide緩存工具類* Created by Trojx on 2016/10/10 0010.*/public class GlideCacheUtil {private static GlideCacheUtil inst;public static GlideCacheUtil getInstance() {if (inst == null) {inst = new GlideCacheUtil();}return inst;}/*** 清除圖片磁盤緩存*/public void clearImageDiskCache(Context context) {try {if (Looper.myLooper() == Looper.getMainLooper()) {new Thread(new Runnable() {@Overridepublic void run() {Glide.get(context).clearDiskCache();// BusUtil.getBus().post(new GlideCacheClearSuccessEvent());}}).start();} else {Glide.get(context).clearDiskCache();}} catch (Exception e) {e.printStackTrace();}}/*** 清除圖片內(nèi)存緩存*/public void clearImageMemoryCache(Context context) {try {if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主線程執(zhí)行Glide.get(context).clearMemory();}} 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);}/*** 獲取Glide造成的緩存大小** @return CacheSize*/public String getCacheSize(Context context) {try {return getFormatSize(getFolderSize(new File(context.getCacheDir() + "/"+InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR)));} catch (Exception e) {e.printStackTrace();}return "";}/*** 獲取指定文件夾內(nèi)所有文件大小的和** @param file file* @return size* @throws Exception*/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;}/*** 刪除指定目錄下的文件,這里用于緩存的刪除** @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();}}}/*** 格式化單位** @param size size* @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";}}通過它就能實現(xiàn)一個清除圖片緩存的功能,在應(yīng)用中實現(xiàn)的效果如下:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答
圖片精選