文件工具类 FileUtil.java 主要获取文件的Cache目录

/**
* 获取文件的Cache目录
/*

public class FileUtil {

    private static final String CACHE = "cache";
    private static final String ICON = "icon";
    private static final String ROOT = "kooshop";

    /** * 获得文件目录 * @param str * @return */
    public static File getDir(String str){
        StringBuilder sb = new StringBuilder();
        if(isSDCardAvailable()){
            sb.append(Environment.getExternalStorageDirectory().getAbsolutePath());
            sb.append(File.separator);
            sb.append(ROOT);
            sb.append(str);
        }else {
            File cache = Util.getContext().getCacheDir();
            sb.append(cache.getAbsolutePath());
            sb.append(File.separator);
            sb.append(str);
        }

        File dir = new File(sb.toString());
        if(!dir.exists() || !dir.isDirectory()){
            dir.mkdirs();
        }
        return dir;
    }

    /** * 获得缓存目录 * @return */
    public static File getCacheDir(){
        return getDir(CACHE);
    }
    /** * 获得实际图片位置 * @return */
    public static File getIconDir(){
        return getDir(ICON);
    }

    /** * 判断sd卡是否可用 * @return */
    private static boolean isSDCardAvailable() {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return true;
        }
        return false;
    }

}

你可能感兴趣的:(android,缓存,cache获取,file获取)