Java Code Examples for android.content.Context#getExternalCacheDir()

The following examples show how to use android.content.Context#getExternalCacheDir() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DiskFileUtils.java    From cube-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: Cache.java    From tns-core-modules-widgets with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: IoUtils.java    From Pioneer with Apache License 2.0 6 votes vote down vote up
/**
 * 获得在缓存存储中的文件
 *
 * @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);
    }
}
 
Example 4
Source File: RLSysUtil.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
/**
 * @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;
        }
    }
}
 
Example 5
Source File: MapTileCache.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: FileUtils.java    From AudioVideoCodec with Apache License 2.0 5 votes vote down vote up
/**
 * 读取缓存目录
 *
 * @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();
    }
}
 
Example 7
Source File: FileUtil.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: PatchUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
private static File getPatchDownloadPatchDir(Context context) {
    File patch = context.getExternalCacheDir();
    if (patch == null || !patch.exists()) {
        return context.getCacheDir();
    }
    return patch;
}
 
Example 9
Source File: jm.java    From letv with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: DiskCacheUtil.java    From FrescoPlus with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: SkinFileUtils.java    From Android-skin-support with MIT License 5 votes vote down vote up
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();
}
 
Example 12
Source File: SkinFileUtils.java    From Android-skin-support with MIT License 5 votes vote down vote up
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();
}
 
Example 13
Source File: AndroidUtils.java    From Theogony with MIT License 5 votes vote down vote up
/**
 * 清空缓存 sdcard中cache目录下
 */
public static boolean clean(Context context) {
    File externalCacheDir = context.getExternalCacheDir();
    File cacheDir = context.getCacheDir();
    delete(externalCacheDir);
    delete(cacheDir);
    return true;
}
 
Example 14
Source File: PrefsUtility.java    From RedReader with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: OkHttpEngine.java    From android-advanced-light with MIT License 5 votes vote down vote up
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();
}
 
Example 16
Source File: SkinFileUtils.java    From Android-skin-support with MIT License 5 votes vote down vote up
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();
}
 
Example 17
Source File: XulSystemUtil.java    From starcor.xul with GNU Lesser General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 18
Source File: Tools.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
public static File getPictureCacheDir(Context ctx) {
    File file = ctx.getExternalCacheDir();
    if (!file.exists()) {
        file.mkdirs();
    }
    return file;
}
 
Example 19
Source File: PersistentBlobProvider.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: MediaUtils.java    From ImagePicker with Apache License 2.0 4 votes vote down vote up
private static String generateTempFilepath(Context context) {
    // 保存到app的cache文件夹下
    return context.getExternalCacheDir() + File.separator + "image_picker_image_uri.jpg";
}