一、在Glide加载图片时,有时需要清理一些缓存,接下来无疑就是清楚缓存的工具类。
/**
* Created by ylzx on 2017/3/10.
*/
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(MyApplication.getContext ().getCacheDir() + "/" + GlideCatchConfig.GLIDE_CARCH_DIR )));
} catch (Exception e) {
e.printStackTrace();
return " 获取失败 " ;
}
}
// 清除 Glide 磁盘缓存,自己获取缓存文件夹并删除方法
public boolean cleanCatchDisk() {
return deleteFolderFile(MyApplication.getContext ().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 (MyApplication.getContext ()).clearDiskCache();
}
}).start();
} else {
Glide.get (MyApplication.getContext ()).clearDiskCache();
}
return true ;
} catch (Exception e) {
e.printStackTrace();
return false ;
}
}
// 清除 Glide 内存缓存
public static boolean clearCacheMemory() {
try {
if (Looper.myLooper () == Looper.getMainLooper ()) { // 只能在主线程执行
Glide.get (MyApplication.getContext ()).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 ;
}
}
}
二、另一个定义的工具类
public class GlideConfiguration implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// 自定义缓存目录
builder.setDiskCache(new InternalCacheDiskCacheFactory(context,
GlideCatchConfig.GLIDE_CARCH_DIR ,
GlideCatchConfig.GLIDE_CATCH_SIZE ));
}
@Override
public void registerComponents(Context context, Glide glide) {
//nil
}
}
三、其次还需再来一个
public class GlideCatchConfig {
// 图片缓存最大容量, 150M ,根据自己的需求进行修改
public static final int GLIDE_CATCH_SIZE = 150 * 1000 * 1000 ;
// 图片缓存子目录
public static final String GLIDE_CARCH_DIR = "image_catch" ;
}
四、最后需配置一下 需要在 AndroidManifest.xml 中声明
<meta-data
android:name="com.yaphetzhao.glidecatchsimple.glide.GlideConfiguration"
android:value="GlideModule" />