Example usage for android.content Context getCacheDir

List of usage examples for android.content Context getCacheDir

Introduction

In this page you can find the example usage for android.content Context getCacheDir.

Prototype

public abstract File getCacheDir();

Source Link

Document

Returns the absolute path to the application specific cache directory on the filesystem.

Usage

From source file:air.com.snagfilms.utils.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * /*from w  w w  .  j a v  a2s.  c  o  m*/
 * @param context
 *            The context to use
 * @param uniqueName
 *            A unique directory name to append to the cache dir
 * @return The cache dir
 */
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 = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath()
                    : context.getCacheDir().getPath();

    return new File(cachePath + File.separator + uniqueName);
}

From source file:com.ethanco.simpleframe.utils.ACache.java

/**
 * //from  w w w  . j  av  a  2s .co  m
 *
 * @param ctx
 * @param cacheName
 * @return
 */
private static File getDefualtCacheDir(Context ctx, String cacheName) {
    File f;
    if (USE_EXTERNAL) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            f = new File(ctx.getExternalCacheDir(), cacheName);
        } else {
            f = new File(ctx.getCacheDir(), cacheName);
        }
    } else {
        f = new File(ctx.getCacheDir(), cacheName);
    }
    return f;
}

From source file:com.rokolabs.app.common.image.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * // w  ww  .j  a v  a2  s  .com
 * @param context
 *            The context to use
 * @param uniqueName
 *            A unique directory name to append to the cache dir
 * @return The cache dir
 */
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 = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() : context.getCacheDir().getPath();
    String cachePath = context.getCacheDir().getPath();
    if (context.getExternalCacheDir().canWrite())
        cachePath = context.getExternalCacheDir().getPath();
    Log.d(TAG, "cache folder is: " + cachePath);
    return new File(cachePath + File.separator + uniqueName);
}

From source file:com.bitfable.ammocache.download.UrlImageDownloader.java

/**
 * Use reflection to enable HTTP response caching on devices that support
 * it. This sample code will turn on the response cache on Ice Cream
 * Sandwich without affecting earlier releases.
 *///from  ww w.ja  v  a 2s  .  com
private void enableHttpResponseCache(Context context) {
    File cacheDir = context.getCacheDir();
    if (cacheDir == null) {
        Log.w(TAG, "cache directory could not be found");
        return;
    }

    try {
        long httpCacheSize = HTTP_CACHE_SIZE;
        File httpCacheDir = new File(cacheDir, HTTP_CACHE_FILE_NAME);
        Class.forName("android.net.http.HttpResponseCache").getMethod("install", File.class, long.class)
                .invoke(null, httpCacheDir, httpCacheSize);
    } catch (Exception httpResponseCacheNotAvailable) {
        Log.v(TAG, "HttpResponseCache is not available");
    }
}

From source file:com.layer.atlas.cells.GeoCell.java

private File getTileFile(Context context) {
    String fileDir = context.getCacheDir() + File.separator + "geo";
    String fileName = String.format("%f_%f.png", lat, lon);
    return new File(fileDir, fileName);
}

From source file:com.st.cms.utils.ACache.java

/**
 * /*from   w  ww  . j  av a 2s .  c o m*/
 * 
 * @param ctx
 * @param cacheName
 * @return
 */
public static ACache get(Context ctx, String cacheName) {
    // ??ACache(new File()????)
    // /data/data/app-package-name/cache/ACache
    File f = new File(ctx.getCacheDir(), cacheName);
    return get(f, MAX_SIZE, MAX_COUNT);
}

From source file:org.ado.minesync.json.JsonWorldManager.java

public JsonWorldManager(Context context) {
    dropboxFileManager = new DropboxFileManager(AppConfiguration.getDropboxAccountManager(context),
            context.getCacheDir());
    dbHelper = MineSyncDbOpenHelper.getInstance(context);
    jsonWorldsAdapter = new JsonWorldsAdapter();
}

From source file:com.theappbusiness.whoswho.utils.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * /*from w  ww .j ava2  s .  c  om*/
 * @param context
 *            The context to use
 * @param uniqueName
 *            A unique directory name to append to the cache dir
 * @return The cache dir
 */
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
    String cachePath = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable()) {
        if (getExternalCacheDir(context) != null) {
            cachePath = getExternalCacheDir(context).getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    return new File(cachePath + File.separator + uniqueName);
}

From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static File getBestAvailableCacheRoot(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // In KitKat we can query multiple devices.
        // TODO: optimize for stability instead of picking first one
        File[] roots = context.getExternalCacheDirs();
        if (roots != null) {
            for (File root : roots) {
                if (root == null) {
                    continue;
                }//from   ww w.  j a  va2s  . c o m

                if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(root))) {
                    return root;
                }
            }
        }

    } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // Pre-KitKat, only one external storage device was addressable
        return context.getExternalCacheDir();
    }

    // Worst case, resort to internal storage
    return context.getCacheDir();
}

From source file:com.android.yijiang.kzx.http.FileAsyncHttpResponseHandler.java

/**
 * Used when there is no file to be used when calling constructor
 *
 * @param context Context, must not be null
 * @return temporary file or null if creating file failed
 *//*from ww w .  ja v  a 2  s  .  co m*/
protected File getTemporaryFile(Context context) {
    assert (context != null);
    try {
        return File.createTempFile("temp_", "_handled", context.getCacheDir());
    } catch (IOException e) {
        Log.e(LOG_TAG, "Cannot create temporary file", e);
    }
    return null;
}