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.wo.bitmap.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use//w ww  .  ja  v  a2 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 (ImageFetcher.isDebug) {
        File f1 = getExternalCacheDir(context);
        File f2 = context.getCacheDir();
        Log.d(ImageFetcher.TAG,
                "ExternalDir is null ? " + (f1 == null) + "; CacheDir is null ? " + (f2 == null));
    }
    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.boko.vimusic.cache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise)
 * //from  w w w.  j ava 2 s.co  m
 * @param context
 *            The {@link Context} to use
 * @param uniqueName
 *            A unique directory name to append to the cache directory
 * @return The cache directory
 */
public static final File getDiskCacheDir(final Context context, final String uniqueName) {
    // getExternalCacheDir(context) returns null if external storage is not
    // ready
    final String cachePath = getExternalCacheDir(context) != null ? getExternalCacheDir(context).getPath()
            : context.getCacheDir().getPath();
    return new File(cachePath, uniqueName);
}

From source file:com.slack.slackteam.cache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use/*from  ww w. ja v a 2s.  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
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? context.getCacheDir().getPath()
                    : context.getCacheDir().getPath();

    //        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:github.daneren2005.dsub.util.FileUtil.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static <T extends Serializable> T deserializeCompressed(Context context, String fileName,
        Class<T> tClass) {/*from w  w  w. j av a  2s  . c o m*/
    Input in = null;
    try {
        RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r");

        in = new Input(new InflaterInputStream(new FileInputStream(file.getFD())));
        synchronized (kryo) {
            T result = kryo.readObject(in, tClass);
            return result;
        }
    } catch (FileNotFoundException e) {
        // Different error message
        Log.w(TAG, "No serialization compressed for object from " + fileName);
        return null;
    } catch (Throwable x) {
        Log.w(TAG, "Failed to deserialize compressed object from " + fileName);
        return null;
    } finally {
        Util.close(in);
    }
}

From source file:github.daneren2005.dsub.util.FileUtil.java

public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass,
        int hoursOld) {
    Input in = null;/*from w ww .  j a  v  a 2 s  . c  o  m*/
    try {
        File file = new File(context.getCacheDir(), fileName);
        if (!file.exists()) {
            return null;
        }

        if (hoursOld != 0) {
            Date fileDate = new Date(file.lastModified());
            // Convert into hours
            long age = (new Date().getTime() - fileDate.getTime()) / 1000 / 3600;
            if (age > hoursOld) {
                return null;
            }
        }

        RandomAccessFile randomFile = new RandomAccessFile(file, "r");

        in = new Input(new FileInputStream(randomFile.getFD()));
        synchronized (kryo) {
            T result = kryo.readObject(in, tClass);
            return result;
        }
    } catch (FileNotFoundException e) {
        // Different error message
        Log.w(TAG, "No serialization for object from " + fileName);
        return null;
    } catch (Throwable x) {
        Log.w(TAG, "Failed to deserialize object from " + fileName);
        return null;
    } finally {
        Util.close(in);
    }
}

From source file:com.blackbird.utils.ACache.java

public static ACache get(Context ctx, String cacheName) {
    ////from   www  .  j  a va  2  s  .c o  m
    ///data/data/com.yangfuhai.asimplecachedemo/cache/ACache
    File f = new File(ctx.getCacheDir(), cacheName);
    return get(f, MAX_SIZE, MAX_COUNT);
}

From source file:com.linked.erfli.library.utils.ACache.java

public static ACache get(Context ctx, String cacheName) {
    //??ACache(new File()????)
    File f = new File(ctx.getCacheDir(), cacheName);
    return get(f, MAX_SIZE, MAX_COUNT);
}

From source file:com.example.mohmurtu.registration.imagesUtil.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use/*ww  w.j a  v 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
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath()
                    : context.getCacheDir().getPath();
    System.out.println("Cache Path: " + cachePath + File.separator + uniqueName);

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

From source file:com.just.agentweb.AgentWebUtils.java

static void clearCache(final Context context, final int numDays) {
    Log.i("Info", String.format("Starting cache prune, deleting files older than %d days", numDays));
    int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
    Log.i("Info", String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
}

From source file:ustc.bitmap.imagecache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use/*from   www.  j  av a  2s  .  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 =context.getCacheDir().getPath();
    return new File(cachePath + File.separator + uniqueName);
}