Example usage for android.content Context getExternalCacheDir

List of usage examples for android.content Context getExternalCacheDir

Introduction

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

Prototype

@Nullable
public abstract File getExternalCacheDir();

Source Link

Document

Returns absolute path to application-specific directory on the primary shared/external storage device where the application can place cache files it owns.

Usage

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 w  ww.  j  a  va  2s  . co 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.xperia64.timidityae.Globals.java

@SuppressLint({ "NewApi", "SdCardPath" })
public static File getExternalCacheDir(Context c) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        return c.getExternalCacheDir();
    } else {//w  w  w  . ja v  a 2 s . c  o m
        return new File("/sdcard/Android/data/com.xperia64.timidityae/cache/");
    }
}

From source file:com.jasper.image.imagemanager.imagehelper.imageEngine.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context    The context to use//  w  w w  . j av  a  2  s .c o m
 * @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
    if (context == null) {
        return null;
    }
    boolean hasSDCard = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable();
    String cachePath = null;

    if (hasSDCard) {
        File file = context.getExternalCacheDir();
        if (file != null) {
            cachePath = file.getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);
}

From source file:com.klinker.android.dream.util.CacheHelper.java

/**
 * Get the cache file path for a given location url
 * @param context the current application context
 * @param location the location url we will hash for the file name
 * @return the full path to the cached image
 *///from  w w w.ja  v  a 2  s  . c  om
public String getCacheLocationForImage(Context context, String location) {
    if (location == null) {
        return null;
    } else {
        return context.getExternalCacheDir() + "/" + hash(location);
    }
}

From source file:org.couchtatertot.helper.PosterCache.java

private PosterCache(Context c) {
    this.cacheDir = new File(c.getExternalCacheDir(), cacheFolder);
    this.cacheDir.mkdirs();
    int memClass = ((ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    // use half of the memory unless we have less then 32MB
    int cacheSize = memClass * 1024 * 1024 / 2;
    if (memClass < 32)
        cacheSize = memClass * 1024 * 1024 / 4;
    this.memCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override//from w w  w.ja  v a 2 s  .  c  o m
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };
}

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

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * //w  ww  . ja  va  2s.  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
    //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:org.sickstache.helper.BannerCache.java

private BannerCache(Context c) {
    c = c.getApplicationContext();//from   w ww. j  ava  2s  .  c o  m
    this.cacheDir = new File(c.getExternalCacheDir(), cacheFolder);
    if (this.cacheDir.exists() == false)
        this.cacheDir.mkdirs();
    int memClass = ((ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    // use half of the memory unless we have less then 32MB
    int cacheSize = memClass * 1024 * 1024 / 2;
    if (memClass < 32)
        cacheSize = memClass * 1024 * 1024 / 4;
    this.memCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };
}

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  w  w w  . ja v  a2 s .co m

    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:com.android.project.imagefetcher.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * // w ww.  ja v a  2s  . 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);

    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:org.jinzora.util.DrawableManager.java

private File getThumbDirectory(Context context) {
    File base;//from w  w  w  . ja  v  a2 s . co  m
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        base = context.getExternalCacheDir();
    } else {
        base = new File(Environment.getExternalStorageDirectory(), context.getPackageName() + "/cache");
    }
    return new File(base, "thumbs");
}