Android Method of Obtaining Application Cache Size and Cleaning Cache
- 2021-10-13 08:32:32
- OfStack
As shown below:
package com.lucasey.littleant.frame;
/**
* Wen Piece Name : FileCacheUtils.java
* Describe State : The main function is to clear the inside / External cache, clear database, clear sharedPreference , clear files And clear the custom directory
* */
import java.io.File;
import java.math.BigDecimal;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
/** This application data cleanup manager */
public class FileCacheUtils {
/**
* * Clear the internal cache of this application (/data/data/com.xxx.xxx/cache) * *
*
* @param context
*/
public static void cleanInternalCache(Context context) {
deleteFilesByDirectory(context.getCacheDir());
}
/**
* * Clear all databases of this application (/data/data/com.xxx.xxx/databases) * *
*
* @param context
*/
public static void cleanDatabases(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/databases"));
}
/**
* * Clear this application SharedPreference(/data/data/com.xxx.xxx/shared_prefs) *
*
* @param context
*/
public static void cleanSharedPreference(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/shared_prefs"));
}
/**
* * Clear this application database by name * *
*
* @param context
* @param dbName
*/
public static void cleanDatabaseByName(Context context, String dbName) {
context.deleteDatabase(dbName);
}
/**
* * Clear /data/data/com.xxx.xxx/files Content under * *
*
* @param context
*/
public static void cleanFiles(Context context) {
deleteFilesByDirectory(context.getFilesDir());
}
/**
* * Clear the outside cache Content under (/mnt/sdcard/android/data/com.xxx.xxx/cache)
*
* @param context
*/
public static void cleanExternalCache(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
deleteFilesByDirectory(context.getExternalCacheDir());
}
}
/**
* * Clear the files under the custom path. Be careful when using them. Please don't delete them by mistake. And it only supports file deletion under the directory * *
*
* @param filePath
* */
public static void cleanCustomCache(String filePath) {
deleteFilesByDirectory(new File(filePath));
}
/**
* * Clear all data of this application * *
*
* @param context
* @param filepath
*/
public static void cleanApplicationData(Context context, String... filepath) {
cleanInternalCache(context);
cleanExternalCache(context);
cleanDatabases(context);
cleanSharedPreference(context);
cleanFiles(context);
if (filepath == null) {
return;
}
for (String filePath : filepath) {
cleanCustomCache(filePath);
}
}
/**
* * Delete method Only files in a folder will be deleted here. If the incoming directory Is a file and will not be processed * *
*
* @param directory
*/
private static void deleteFilesByDirectory(File directory) {
if (directory != null && directory.exists() && directory.isDirectory()) {
for (File item : directory.listFiles()) {
item.delete();
}
}
}
// Get a file
//Context.getExternalFilesDir() --> SDCard/Android/data/ Package name of your application /files/ Directory, 1 General release 1 Some data stored for a long time
//Context.getExternalCacheDir() --> SDCard/Android/data/ Your application package name /cache/ Directory, 1 Store temporary cache data in general
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 there are files below
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
/**
* Delete files and directories in specified directories
*
* @param deleteThisPath
* @param filepath
* @return
*/
public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
if (!TextUtils.isEmpty(filePath)) {
try {
File file = new File(filePath);
if (file.isDirectory()) {// If there are files below
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFolderFile(files[i].getAbsolutePath(), true);
}
}
if (deleteThisPath) {
if (!file.isDirectory()) {// If it is a file, delete
file.delete();
} else {// Directory
if (file.listFiles().length == 0) {// There are no files or directories under the directory, delete them
file.delete();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* Formatting unit
* @param size
* @return
*/
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";
}
/***
* Gets the application cache size
* @param file
* @return
* @throws Exception
*/
public static String getCacheSize(File file) throws Exception {
return getFormatSize(getFolderSize(file));
}
}