原文链接:https://www.zybuluo.com/Tyhj/note/1144961
当设备储存有限的时候,定时清理APP数据就很重要了
文件清理
文件清理有几种方式,一种是判断文件夹的大小,如果太大了就删除一部分数据,获取文件夹的大小,需要遍历文件夹下面的所有文件来获取大小,文件多了可能比较耗时
//获取文件大小
public static long getFolderSize(File file) throws Exception {
long size = 0;
try {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
// 如果下面还有文件
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
可以根据剩余大小空间来清理文件,比较快
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
long blockSize = sf.getBlockSize();
long availCount = sf.getAvailableBlocks();
long size = availCount * blockSize / (1024 * 1024);
LogUtils.e(剩余空间:", size + "MB");
}
当然也可以根据文件的数量来删除文件
//删除文件
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();
}
清除数据库的数据
public static void cleanDatabases(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/databases"));
}
清除SharedPreference数据
public static void cleanSharedPreference(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/shared_prefs"));
}
缓存清理
获取缓存大小
public static String getTotalCacheSize(Context context) {
//Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
long cacheSize = 0;
try {
cacheSize = getFolderSize(context.getCacheDir());
//Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheSize += getFolderSize(context.getExternalCacheDir());
}
} catch (Exception e) {
e.printStackTrace();
}
return getFormatSize(cacheSize);
}
清除缓存
public static void clearAllCache(Context context) {
deleteDir(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
deleteDir(context.getExternalCacheDir());
//下面两句清理webview网页缓存.但是每次执行都报false,我用的是魅蓝5.1的系统,后来发现且/data/data/应用package目录下找不到database文///件夹 不知道是不是个别手机的问题,
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
}
}
格式化单位
/**
* 格式化单位
* @param size
*/
public 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";
}