import android.content.Context;
import android.os.Environment;
import java.io.File;
/**
* 緩存的工具類
* 如:獲取應(yīng)用緩存根目錄,imageloader,頭像根目錄,獲取應(yīng)用緩存大小,清除緩存
*
*/
public class CacheUtils {
private static final String IMAGELOADER_SEPARATOR = "imageloader";
private static final String AVATAR_SEPARATOR = "avatar";
/**
* 得到該應(yīng)用的緩存根目錄
*
* @param context 上下文
* @param appName 應(yīng)用名稱
* @return 應(yīng)用的緩存根目錄
*/
public static File getRootCache(Context context, String appName) {
String appDirRootPath ;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
appDirRootPath = Environment.getExternalStorageDirectory().getPath()
+ File.separator + appName + File.separator;
} else {
appDirRootPath = context.getCacheDir().getPath()
+ File.separator + appName + File.separator;
}
File dirCache = new File(appDirRootPath);
if (!dirCache.exists()) {
dirCache.mkdirs();
}
return dirCache;
}
/**
* 得到該應(yīng)用的imageloader的緩存目錄
*
* @param context 上下文
* @param appName 應(yīng)用名稱
* @return imageloader的緩存目錄
*/
public static File getImageLoaderCache(Context context, String appName) {
String imageLoaderCachePath = getRootCache(context, appName).getPath() + File.separator + IMAGELOADER_SEPARATOR + File.separator;
File file = new File(imageLoaderCachePath);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
/**
* 得到頭像的image目錄
*
* @param context 上下文
* @param appName 應(yīng)用名稱
* @return 頭像的image目錄
*/
public static File getAvatarCache(Context context, String appName) {
String avatarCachePath = getRootCache(context, appName).getPath() + File.separator + AVATAR_SEPARATOR + File.separator;
File file = new File(avatarCachePath);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
/**
* 獲取應(yīng)用緩存大小
*
* @param context 上下文
* @return 緩存大小,如"10.0MB"
*/
public static String getCacheSize(Context context) {
File file = context.getCacheDir();
if (file != null && file.exists()) {
double mb = FileUtils.getFolderSize(file);
String temp = FileUtils.getFormatSize(mb);
return temp;
} else {
return "0MB";
}
}
/**
* 清除緩存
*
* @param context 上下文
*/
public static void clearCache(Context context) {
File file = context.getCacheDir();
FileUtils.deleteFiles(file);
}
}
[Android][工具類]CacheUtils
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- [TOC] 前言 Android SDK原生 API中,有一些常用的工具類,運用得當(dāng)可以省事省力省時,何況還是An...
- 前言 相信大部分仁兄在使用系統(tǒng)Toast的時候,都感覺不太盡如人意,因為系統(tǒng)Toast顯示的位置比較固定,并且字體...
- 1. IMEI IMEI(International Mobile Equipment Identity)是國際移...
- 文章來源:微信公眾號 職場修煉寶典(ID:zcxlbd) 職場如戰(zhàn)場,十八般武藝不敵一招致命是常有的事情,尤其是剛...