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:me.vijayjaybhay.galleryview.cache.DiskImageCache.java

/**
 * Creates a unique subdirectory of the designated app cache directory. Tries to use external
 * but if not mounted, falls back on internal storage.
 * @param context Application context/*from www.  ja  va2  s.c o m*/
 * @param uniqueName Name of directory
 * @return File object corresponding to directory
 */

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() ? DISK_CACHE_SUBDIR : context.getCacheDir().getPath();

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

From source file:id.wibisana.priadimulia.imagecaching.cache.ImageCache.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    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.wowcow.chat10.imagecache.ImageCache.java

/**
 * Get the external app cache directory.
 * /*from w  w w. j  a  v a  2  s .c o  m*/
 * @param context
 *          The context to use
 * @return The external cache dir
 */
public static File getExternalCacheDir(Context context) {
    if (context.getExternalCacheDir() == null) {
        return context.getCacheDir();
    } else {
        return context.getExternalCacheDir();
    }
}

From source file:org.kontalk.util.MediaStorage.java

/** Writes a thumbnail of a media to the internal cache. */
public static File cacheThumbnail(Context context, Uri media, String filename, boolean forNetwork)
        throws IOException {
    File file = new File(context.getCacheDir(), filename);
    cacheThumbnail(context, media, file, forNetwork);
    return file;/*from   w w  w .j av a2s  .c o m*/
}

From source file:com.test.displaybitmaps.imagemanager.ImageCache.java

/**
 * Get a usable cache directory (internal).
 * //from  w  w  w. j a  va  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) {
    // use internal cache dir
    final String cachePath = context.getCacheDir().getPath();

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

From source file:com.data.pack.Util.java

public static void trimCache(Context context) {
    try {//from   w ww . j av  a 2  s. co m
        File cacheDir = context.getCacheDir();

        File[] files = cacheDir.listFiles();

        if (files != null) {
            for (File file : files) {
                String fname = file.getName();
                if (!fname.equalsIgnoreCase("completed_exercise_de.mp4")
                        && !fname.equalsIgnoreCase("completed_exercise.mp4")

                        && !fname.equalsIgnoreCase("next_exercise_de.mp4")
                        && !fname.equalsIgnoreCase("next_exercise.mp4")
                        && !fname.equalsIgnoreCase("otherside_exercise_de.mp4")
                        && !fname.equalsIgnoreCase("otherside_exercise.mp4")
                        && !fname.equalsIgnoreCase("recovery_15_de.mp4")
                        && !fname.equalsIgnoreCase("recovery_15.mp4")
                        && !fname.equalsIgnoreCase("recovery_30_de.mp4")
                        && !fname.equalsIgnoreCase("recovery_30.mp4")
                        && !fname.equalsIgnoreCase("stop_exercise_de.mp4")
                        && !fname.equalsIgnoreCase("stop_exercise.mp4") && fname.contains(".mp4")

                ) {
                    if (file.exists())
                        file.delete();
                }
            }
        }
    } catch (Exception e) {
        // TODO: handle exception
        String st = e.toString();
        st = st + "";
    }
}

From source file:com.twitt4droid.util.Images.java

/**
 * Initializes the disk cache when is closed or null.
 * //from  w  w  w  . j  av a  2 s  .c  o  m
 * @param context the application context.
 */
private static void intDiskCacheIfNeeded(Context context) {
    if (DISK_CACHE == null || DISK_CACHE.isClosed()) {
        try {
            long size = 1024 * 1024 * 10;
            String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                    || Files.isExternalStorageRemovable() ? Files.getExternalCacheDir(context).getPath()
                            : context.getCacheDir().getPath();
            File file = new File(cachePath + File.separator + IMAGE_CACHE_DIR);
            DISK_CACHE = DiskLruCache.open(file, 1, 1, size); // Froyo sometimes fails to initialize
        } catch (IOException ex) {
            Log.e(TAG, "Couldn't init disk cache", ex);
        }
    }
}

From source file:com.blork.anpod.util.BitmapUtils.java

private static void manageCache(String slug, Context context) {
    String filename = slug + ".jpg";
    File folder;//from   w w w.ja v  a 2 s .  c  om
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        folder = new File(Environment.getExternalStorageDirectory() + File.separator + "Android"
                + File.separator + "data" + File.separator + "com.blork.anpod" + File.separator + "cache");
    } else {
        folder = context.getCacheDir();
    }

    int cacheSize = 15;
    Log.w("", "current file: " + filename);
    Log.w("", "Managing cache");
    File[] files = folder.listFiles();
    if (files == null || files.length <= cacheSize) {
        Log.w("", "Cache size is fine");
        return;
    }

    int count = files.length;

    Arrays.sort(files, new Comparator<File>() {
        public int compare(File f1, File f2) {
            return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
        }
    });

    for (File f : files) {
        if (count > cacheSize && !filename.equals(f.getName())) {
            Log.w("", "Deleting " + f.getName());
            f.delete();
            count--;
        }
    }
}

From source file:com.lxh.util.image.OtherUtils.java

/**
 * ??//from  ww  w.  j  a  va2s  .  co  m
 * 
 * <pre>
 * SDCard
 * 
 * </pre>
 * 
 * @param context android.content.Context
 * @param dirName ???
 * @return APPapp_cache_path/dirName
 */
public static String getDiskCacheDir(Context context, String dirName) {
    String cachePath = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File externalCacheDir = context.getExternalCacheDir();
        if (externalCacheDir != null) {
            cachePath = externalCacheDir.getPath();
        }
    }
    if (cachePath == null) {
        File cacheDir = context.getCacheDir();
        if (cacheDir != null && cacheDir.exists()) {
            cachePath = cacheDir.getPath();
        }
    }
    return cachePath + File.separator + dirName;
}

From source file:org.goodev.droidddle.utils.IOUtil.java

public static File getBestAvailableCacheRoot(Context context) {
    File[] roots = ContextCompat.getExternalCacheDirs(context);
    if (roots != null) {
        for (File root : roots) {
            if (root == null) {
                continue;
            }//from   w ww  . j ava 2  s. c om

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

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