1、创建内部存储缓存文件
/**
* Returns the absolute path to the cache file on the filesystem. These* files will be
* ones that get deleted first when the device runs on storage.
* @param context
* Global information about an application environment
* @param name
* File name
* @return Returns the absolute path to the application specific cache
* directory on the
* filesystem. Returns null if external storage is not currently
* mounted.
*/
public static File getInternalCacheFile(Context context, String name) {
File dir = context.getCacheDir();
if (dir == null) {
return null;
}
if (!dir.exists()) {
dir.mkdirs();
}
return new File(context.getCacheDir(), name);
}
2、创建外部存储缓存文件
/**
* Returns the absolute path to the cache file on the external filesystem
* @param context
* Global information about an application environment
* @param name
* File name
* @return Returns the path of the cache file on external storage. Returns
* null if
* external storage is not currently mounted.
*/
public static File getExternalCacheFile(Context context, String name) {
File dir = getExternalCacheDir(context);
if (dir == null) {
return null;
}
return new File(getExternalCacheDir(context), name);
}
public static File getExternalCacheDir(Context context) {
//判断是否挂载外部存储
boolean isSDCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (!isSDCardExist) {
return null;
}
final File externalCacheDir = new File(
Environment.getExternalStorageDirectory(), "/Android/data/"
+ context.getPackageName() + "/cache/");
if (!externalCacheDir.exists()) {
externalCacheDir.mkdirs();
}
return externalCacheDir;
}