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:cn.com.loopj.android.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  w ww.  j  a v  a 2s  .c  om
protected File getTemporaryFile(Context context) {
    Utils.asserts(context != null, "Tried creating temporary file without having Context");
    try {
        return File.createTempFile("temp_", "_handled", context.getCacheDir());
    } catch (IOException e) {
        AsyncHttpClient.log.e(LOG_TAG, "Cannot create temporary file", e);
    }
    return null;
}

From source file:ac.robinson.paperchains.SoundCloudUploadTask.java

private File getAppIconCacheFile(Context context) {
    if (context == null) {
        return null;
    }//  w w w  . j av  a 2 s  .  c o  m

    // if we've already created the file, just return it
    File cacheFile = new File(context.getCacheDir(), "paperchains.png");
    if (cacheFile.exists()) {
        return cacheFile;
    }

    // otherwise, load from assets
    try {
        InputStream inputStream = context.getAssets().open("paperchains.png");
        // suppressed as it requires API level 19
        //noinspection TryFinallyCanBeTryWithResources
        try {
            FileOutputStream outputStream = new FileOutputStream(cacheFile);
            //noinspection TryFinallyCanBeTryWithResources
            try {
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, len);
                }
            } finally {
                outputStream.close();
            }
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        return null;
    }
    return cacheFile;
}

From source file:com.laurencedawson.image_management.ImageManager.java

/**
 * Get the cache directory to store images in
 * @param context The calling {@link Context}
 * @return a {@link File} to save images in
 *///  ww  w.  ja  v  a 2  s.c  om
private static File getCacheDir(final Context context) {
    if (context != null) {
        // Try to grab a reference to the external cache dir
        final File directory = context.getExternalCacheDir();

        // If the file is OK, return it
        if (directory != null) {
            return directory;
        }

        // If that fails, try to get a reference to the internal cache
        // This is a rare edge case but occasionally users don't have external
        // storage / their SDCard is removed / the folder is corrupt
        else {
            return context.getCacheDir();
        }
    }
    return null;
}

From source file:com.onedollar.image.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * //from   w w w  .j av  a2 s . 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
    String externalCacheDir = null;
    try {
        externalCacheDir = getExternalCacheDir(context).getPath();
    } catch (Exception e) {

        try {
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    if (TextUtils.isEmpty(externalCacheDir)) {
        externalCacheDir = context.getCacheDir().getPath();
    }
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? externalCacheDir : context.getCacheDir().getPath();

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

From source file:com.google.cloud.backend.core.CloudBackendFragment.java

private RequestQueue newRequestQueue(Context context) {
    // define cache folder
    File rootCache = context.getExternalCacheDir();
    if (rootCache == null) {
        rootCache = context.getCacheDir();
    }/*from www.j a v  a2 s  .c om*/

    File cacheDir = new File(rootCache, DEFAULT_CACHE_DIR);
    cacheDir.mkdirs();

    HttpStack stack = new HurlStack();
    Network network = new BasicNetwork(stack);
    DiskBasedCache diskBasedCache = new DiskBasedCache(cacheDir, DEFAULT_DISK_USAGE_BYTES);
    RequestQueue queue = new RequestQueue(diskBasedCache, network);
    queue.start();

    return queue;
}

From source file:net.oddsoftware.android.html.HttpCache.java

public HttpCache(Context context) {
    mCacheDirectory = context.getCacheDir();
    mContentResolver = context.getContentResolver();
}

From source file:com.fdwills.external.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  www.  j  av a  2  s . c  o  m*/
protected File getTemporaryFile(Context context) {
    AssertUtils.asserts(context != null, "Tried creating temporary file without having Context");
    try {
        // not effective in release mode
        assert context != null;
        return File.createTempFile("temp_", "_handled", context.getCacheDir());
    } catch (IOException e) {
        Log.e(LOG_TAG, "Cannot create temporary file", e);
    }
    return null;
}

From source file:com.lightbox.android.cache.ApiCache.java

private ApiCache() {
    Context context = AndroidUtils.getApplicationContext();
    if (context == null) {
        throw new IllegalStateException(
                "You must set an Application context in ANdroidUtils before attempting to use the ApiCache.");
    }//  w  w  w .  j a v a 2s  . c  o m

    CACHE_DIR = context.getCacheDir().getAbsolutePath() + "/";
}

From source file:com.android.project.imagefetcher.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * /*from  ww  w.  j  a  v  a2s  .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();
    //
    // return new File(cachePath + File.separator + uniqueName);

    File cacheDir;
    // Choose chache directory
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir = new File(context.getExternalCacheDir(), uniqueName);
    else
        cacheDir = context.getCacheDir();
    // File in cache directory
    File f = new File(cacheDir.getPath() + File.separator + uniqueName);
    return f;
}

From source file:my.example.onekeycleaner.imgcache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use/*from www.  j  a  va 2  s. c om*/
 * @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();

    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            ? getExternalCacheDir(context).getPath()
            : context.getCacheDir().getPath();

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