/** * 返回 应用的缓存目录 * * @param context * @param cacheDir * @return */ public static File getOwnCacheDirectory(Context context, String cacheDir) { File appCacheDir = null; if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) { appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir); } if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) { appCacheDir = context.getCacheDir(); } return appCacheDir; }
/** * 返回 应用的缓存目录 * ,优先在sdcard上("/Android/data/[app_package_name]/cache"),需要sdcard操作权限,如果失败返回android系统的缓存目录 * * @param context Application context * @return Cache {@link File directory} */ public static File getCacheDirectory(Context context) { File appCacheDir = null; if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) { appCacheDir = getExternalCacheDir(context); } if (appCacheDir == null) { appCacheDir = context.getCacheDir(); } if (appCacheDir == null) { EasyLog.w("获取应用缓存目录失败"); } return appCacheDir; }
/** * Returns application cache directory. Cache directory will be created on SD card * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted and app has appropriate * permission. Else - Android defines cache directory on device's file system. * * @param context Application context * @return Cache {@link File directory} */ public static File getCacheDirectory(Context context) { File appCacheDir = null; if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) { appCacheDir = getExternalCacheDir(context); } if (appCacheDir == null) { appCacheDir = context.getCacheDir(); } if (appCacheDir == null) { L.w("Can't define system cache directory! The app should be re-installed."); } return appCacheDir; }