项目里遇到缓存路径的时候之前基本都是自己随意建立文件夹,这样的缺点是,当在系统总清除缓存数据,或者卸载app时,之前的缓存并不能一块清除,其实andriodsdk提供了获取基于项目的sd卡缓存路径和内存的缓存路径的方法来解决这个问题
public static File getDiskCacheDir(Context context) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
File file = new File(cachePath + File.separator + CROPIMAGEPATH);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
这段代码逻辑就是优先存储sd卡,判断sd是否能用,如果能用则获取sd卡缓存路径,否则获取内存缓存路径。
如果有错误欢迎指正交流
qq:1251600274
email:1251600274@qq.com