下面列出了android.content.Context#getExternalCacheDir ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get the external application cache directory.
*
* @param context The context to use
* @return The external cache folder : /storage/sdcard0/Android/data/com.srain.sdk/cache
*/
@TargetApi(Build.VERSION_CODES.FROYO)
public static File getExternalCacheDir(Context context) {
if (Version.hasFroyo()) {
File path = context.getExternalCacheDir();
// In some case, even the sd card is mounted, getExternalCacheDir will return null, may be it is nearly full.
if (path != null) {
return path;
}
}
// Before Froyo or the path is null, we need to construct the external cache folder ourselves
final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}
/**
* Get the external app cache directory.
*
* @param context The context to use
* @return The external cache dir
*/
@TargetApi(VERSION_CODES.FROYO)
public static File getExternalCacheDir(Context context) {
if (Utils.hasFroyo()) {
if (Utils.hasKitKat() ||
context.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, android.os.Process.myPid(), android.os.Process.myUid()) == PackageManager.PERMISSION_GRANTED) {
return context.getExternalCacheDir();
}
return null;
}
// Before Froyo we need to construct the external cache dir ourselves
final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}
/**
* @param context
* @return
*/
public static String getExternalCacheDir(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
File f1 = context.getExternalCacheDir();
if (f1 != null) {
return f1.getPath();
} else {
return null;
}
} else {
final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
File f2 = Environment.getExternalStorageDirectory();
if (f2 != null) {
return f2.getPath() + cacheDir;
} else {
return null;
}
}
}
/**
* 获得在缓存存储中的文件
*
* @param path
* @return
*/
public static File fromCacheDir(Context context, String path) {
if (path != null && path.startsWith(File.separator)) {
path = path.substring(File.separator.length());
}
File cacheDir = context.getExternalCacheDir();
File rollback = cacheDir;
if (cacheDir != null && !ensureDirsExist(cacheDir)) {
cacheDir = null;
}
if (cacheDir == null) {
// maybe null
cacheDir = context.getCacheDir();
}
if (cacheDir == null) {
cacheDir = rollback;
}
if (path == null) {
return cacheDir;
} else {
return new File(cacheDir, path);
}
}
/**
* 读取缓存目录
*
* @param context :context
* @return String
*/
public static String getDiskCachePath(Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
if (context.getExternalCacheDir() == null) {
return "";
}
return context.getExternalCacheDir().getPath();
} else {
return context.getCacheDir().getPath();
}
}
public static File getPictureCacheDir(Context ctx) {
File file = ctx.getExternalCacheDir();
if (!file.exists()) {
file.mkdirs();
}
return file;
}
private static String getExternalCacheDir(Context context) {
File dir = context.getExternalCacheDir();
if (dir == null) {
return null;
}
if (!dir.mkdirs() && !dir.exists()) {
return null;
}
return dir.getPath();
}
private static String getCacheDir(Context context) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File cacheDir = context.getExternalCacheDir();
if (cacheDir != null && (cacheDir.exists() || cacheDir.mkdirs())) {
return cacheDir.getAbsolutePath();
}
}
return context.getCacheDir().getAbsolutePath();
}
private OkHttpEngine(Context context) {
File sdcache = context.getExternalCacheDir();
int cacheSize = 10 * 1024 * 1024;
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));
mOkHttpClient=builder.build();
mHandler = new Handler();
}
public static String pref_cache_location(final Context context,
final SharedPreferences sharedPreferences) {
File defaultCacheDir = context.getExternalCacheDir();
if (defaultCacheDir == null) {
defaultCacheDir = context.getCacheDir();
}
return getString(R.string.pref_cache_location_key,
defaultCacheDir.getAbsolutePath(),
context, sharedPreferences);
}
/**
* 清空缓存 sdcard中cache目录下
*/
public static boolean clean(Context context) {
File externalCacheDir = context.getExternalCacheDir();
File cacheDir = context.getCacheDir();
delete(externalCacheDir);
delete(cacheDir);
return true;
}
private static String getCacheDir(Context context) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File cacheDir = context.getExternalCacheDir();
if (cacheDir != null && (cacheDir.exists() || cacheDir.mkdirs())) {
return cacheDir.getAbsolutePath();
}
}
return context.getCacheDir().getAbsolutePath();
}
private static String getCacheDir(Context context) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File cacheDir = context.getExternalCacheDir();
if (cacheDir != null && (cacheDir.exists() || cacheDir.mkdirs())) {
return cacheDir.getAbsolutePath();
}
}
return context.getCacheDir().getAbsolutePath();
}
public static File getDiskLruCacheDir(Context context) {
if (context == null)
throw new FPNullPointerException("context can not be null");
if (!(context instanceof Application))
context = context.getApplicationContext();
File cacheDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cacheDir = getSDFreeSize() > 100 ? context.getExternalCacheDir() : context.getCacheDir();
} else {
cacheDir = context.getCacheDir();
}
return cacheDir;
}
public static File b(boolean z) {
Context c = hn.a().c();
File file = null;
if (z && "mounted".equals(Environment.getExternalStorageState()) && (VERSION.SDK_INT >= 19 || c.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") == 0)) {
file = c.getExternalCacheDir();
}
if (file == null) {
return c.getCacheDir();
}
return file;
}
private static File getPatchDownloadPatchDir(Context context) {
File patch = context.getExternalCacheDir();
if (patch == null || !patch.exists()) {
return context.getCacheDir();
}
return patch;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static File getWebCacheFile(Context context) {
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return null;
}
File file = new File(context.getExternalCacheDir(), CacheConstant.CACHE_DIR_WEB);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
/**
* Creates a unique subdirectory of the designated app cache directory. Tries to use external
* but if not mounted, falls back on internal storage.
*/
public static File getDiskCacheDir(Context context, String uniqueName) {
// Check if media is mounted or storage is built-in, if so, try and use external cache dir
// otherwise use internal cache dir
final String cachePath =
context.getExternalCacheDir() != null && (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| (!Environment.isExternalStorageRemovable()))
? context.getExternalCacheDir().getPath()
: context.getCacheDir().getPath();
Log.i(TAG, "cachePath: '" + cachePath + "'");
return new File(cachePath, uniqueName);
}
private static String generateTempFilepath(Context context) {
// 保存到app的cache文件夹下
return context.getExternalCacheDir() + File.separator + "image_picker_image_uri.jpg";
}
private static @NonNull
File getExternalDir(Context context) throws IOException {
final File externalDir = context.getExternalCacheDir();
if (externalDir == null) throw new IOException("no external files directory");
return externalDir;
}