一个应用安装完成,会有缓存,缓存一般分为内部存储的和外置的,内存中路径为/data/data/pacgage_name/文件,先来看看一般缓存会存在哪些文件中:
(1)缓存文件夹:
/data/data/com.xx/cache
(2)sharePreferance文件夹:
/data/data/com.xx/shared_prefs
(3)DB数据库文件夹:
/data/data/com.xx/databases
(4)files文件夹:(指向同一文件夹)
context.getFilesDir()
/data/data/com.xx/files
(5)外置缓存文件夹:context.getExternalCacheDir()
(6)lib文件夹,一般可以不清理该文件夹:
"/data/data/com.gtafe.zhpj/lib"
一般一个应用的缓存都存在这些文件夹中,接下直接上代码吧,把计算缓存和清除缓存都用一个类封装(带弹框):
/**
* Created by lan.zheng on 2016/9/1.
*/
public class CacheUtil {
private static final String CACHE_DATA_DB = "/data/data/com.gtafe.zhpj/databases";
private static final String CACHE_DATA_SF = "/data/data/com.gtafe.zhpj/shared_prefs";
private static final String CACHE_DATA_CACHE = "/data/data/com.gtafe.zhpj/cache";
private static File lFileDB;
private static File lFileSF;
private static File lFileC;
/**
* 获取所有缓存
* @param context
* @return
* @throws Exception
*/
public static String getTotalCacheSize(Context context) throws Exception {
lFileDB = new File(CACHE_DATA_DB);
lFileSF = new File(CACHE_DATA_SF);
lFileC = new File(CACHE_DATA_CACHE);
//内存缓存DB和SF,cache,files文件
long cacheSizeDB = getFolderSize(lFileDB);
long cacheSizeSF = getFolderSize(lFileSF);
long cacheSizeC = getFolderSize(lFileC);
long cacheSize = cacheSizeDB + cacheSizeSF + cacheSizeC + getFolderSize(context.getFilesDir()); //context.getFilesDir()或者像上面一样直接用路径
//fresco产生的cache,如果需要就加入下面这行代码
//long cacheSizeFresco = Fresco.getImagePipelineFactory().getMainDiskStorageCache().getSize();
cacheSize = cacheSize + cacheSizeFresco;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheSize = cacheSize + getFolderSize(context.getExternalCacheDir());
}
return getFormatSize(cacheSize);
}
/**
* 清除所有缓存
* @param context
* @return
*/
static boolean isClearSuccess = true;
public static void clearAllCache(Context context,ShowClearResultListener showClearResultListener) {
mListener = showClearResultListener; //监听初始化
showWaitingDialog(context); //弹框等待
//清理Fresco的缓存
ImagePipeline imagePipeline = Fresco.getImagePipeline();
imagePipeline.clearCaches();
//清理内存和文件缓存
isClearSuccess = deleteDir(lFileDB);
isClearSuccess = deleteDir(lFileSF);
isClearSuccess = deleteDir(lFileC);
isClearSuccess = deleteDir(context.getFilesDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
isClearSuccess = deleteDir(context.getExternalCacheDir());
}
//TODO 别的缓存
/* if(context.getCacheDir() != null){
deleteDir(context.getCacheDir());
}*/
}
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
// 获取文件
//Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
//Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
public static long getFolderSize(File file) throws Exception {
long size = 0;
try {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
Log.d("test11","file"+i+" = "+fileList[i].toString());
// 如果下面还有文件
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
/**
* 格式化单位
*
* @param size
* @return
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
// return size + "Byte";
return "0.0M";
}
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";
}
/**
* 弹框确认方法
*/
public static void showWaitingDialog(Context context){
//弹框
popupConfirmDialog(context , "清除中...", R.mipmap.ic_clear_cache);
}
private static Dialog modifyDialog;
static ShowClearResultListener mListener;
private static void popupConfirmDialog(Context context, String content, int resId) {
//弹框内容
if (modifyDialog == null) {
View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_waiting_for_opr, null);
TextView modifyContent = (TextView) contentView.findViewById(R.id.dialog_content_text);
modifyContent.setText(content);
ImageView modifyImage = (ImageView)contentView.findViewById(R.id.dialog_content_image);
modifyImage.setImageDrawable(context.getResources().getDrawable(resId));
modifyDialog = new Dialog(context, R.style.clearCacheDialogTheme); //使用自定义的样式
modifyDialog.setCanceledOnTouchOutside(false); //外围点击不消失
modifyDialog.setContentView(contentView);
}
modifyDialog.show();
sleepToWaitClear();
}
static Handler mHandler = new Handler();
private static void sleepToWaitClear(){
mHandler.postDelayed(mRunnableDialogDismiss,1000); //延迟启动
}
private static Runnable mRunnableDialogDismiss = new Runnable() {
@Override
public void run() {
if(modifyDialog != null ){
modifyDialog.dismiss();
modifyDialog = null;
mListener.Clear(isClearSuccess);
}
}
};
/**
* 1秒后回调清除结果
*/
public interface ShowClearResultListener{
void Clear(boolean isClear);
}
}
弹框的样式调用:
封装的弹框代码(圆角代码省略@drawable/dialog_content_radius):
最后就是在主函数中使用了:
(1)获取缓存大小:
String cacheSize=CacheUtil.getTotalCacheSize(this);
cache_size.setText(cacheSize);
(2)清除缓存:
CacheUtil.clearAllCache(UserCenterActivity.this, new CacheUtil.ShowClearResultListener() {
@Override
public void Clear(boolean isClear) {
if(isClear){
cache_size.setText("0.0M");
}
}
});
以上就是清除缓存的方法,最后发现一个问题,在安卓4.4一下的系统中清除缓存后和管理器看到的结果是一样的,但是
4.4以上的系统在管理器显示还是有些缓存没被清除,这个问题有人知道为什么吗?