当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中Glide获取缓存大小并清除缓存图片

Android中Glide获取缓存大小并清除缓存图片

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

通过它就能实现一个清除图片缓存的功能,在应用中实现的效果如下:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网