内容简介:Glide获取缓存大小并清除缓存图片
Glide自带清除缓存的功能,分别对应 Glide.get(context).clearDiskCache();
(清除磁盘缓存)与 Glide.get(context).clearMemory();
(清除内存缓存)
两个方法.其中 clearDiskCache()
方法必须运行在子线程, clearMemory()
方法必须运行在主线程,这是这两个方法所强制要求的,详见源码.
获取Glide缓存空间大小
这个网上也有过一些介绍,但是给出的实现代码存在一些问题,我这里做了一定的修改.
一下方法适合在Glide为默认的缓存目录的情况,不论是内部存储空间还是外部.
因为我们可以通过InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR
与
ExternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR
获取到位于内部与外部存储的缓存文件夹的文件夹名,并通过 context.getCacheDir()
与 context.getExternalCacheDir()
获取内部与外部存储的路径.
进而可以通过遍历文件夹内的文件进行缓存文件大小求和与全部清除.
以下 工具 类在其他的文章中有前辈写过,但是存在一些已知的问题,这里做了一些修改.
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() { @Override public void run() { Glide.get(context).clearDiskCache(); // BusUtil.getBus().post(new GlideCacheClearSuccessEvent()); } }).start(); } else { Glide.get(context).clearDiskCache(); } } catch (Exception e) { e.printStackTrace(); } } /** * 清除图片内存缓存 */ public void clearImageMemoryCache(Context context) { try { if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行 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 ""; } /** * 获取指定文件夹内所有文件大小的和 * * @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"; } }
通过它就能实现一个清除图片缓存的功能,在应用中实现的效果如下:


以上所述就是小编给大家介绍的《Glide获取缓存大小并清除缓存图片》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 注意 Laravel 清除缓存 php artisan cache:clear 的一个坑
- J2Cache 2.4.2 发布,缓存对象不兼容时自动清除
- 如何清除浮动
- 清除 Git 敏感信息
- 清除浮动
- 总结:清除浮动的方法
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Cyberwar
Kathleen Hall Jamieson / Oxford University Press / 2018-10-3 / USD 16.96
The question of how Donald Trump won the 2016 election looms over his presidency. In particular, were the 78,000 voters who gave him an Electoral College victory affected by the Russian trolls and hac......一起来看看 《Cyberwar》 这本书的介绍吧!