[Android][工具類]CacheUtils

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);
    }
}

最后編輯于
?著作權(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容