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:com.brkc.common.image.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use//  w w 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
    boolean test1 = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    boolean test2 = !isExternalStorageRemovable();
    boolean test3 = getExternalCacheDir(context) != null;
    Log.d(TAG, "test1=" + test1);
    Log.d(TAG, "test2=" + test2);
    Log.d(TAG, "getExternalCacheDir(context)=" + getExternalCacheDir(context));
    Log.d(TAG, "context.getCacheDir()=" + context.getCacheDir());
    final String cachePath = (test1 || test2) && test3 ? getExternalCacheDir(context).getPath()
            : context.getCacheDir().getPath();

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

From source file:me.aerovulpe.crawler.ui.GifImageView.java

private static void initGifCache(Context context) {
    synchronized (GifThread.class) {
        if (sGifCache != null)
            return;

        try {/*from   w  w w  .  j ava2 s. c  om*/
            final int appVersion = 1;
            final int maxSize = SettingsFragment.getCurrentCacheValueInBytes(context) / 4;
            sGifCache = SimpleDiskCache.open(new File(context.getCacheDir().getAbsolutePath() + "/gifCache"),
                    appVersion, maxSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.mozilla.gecko.FilePickerResultHandler.java

public FilePickerResultHandler(final FilePicker.ResultHandler handler, final Context context, final int tabId) {
    this.tabId = tabId;
    this.cacheDir = new File(context.getCacheDir(), UPLOADS_DIR);
    this.handler = handler;
}

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  a va 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.android.volley.cache.ACache.java

private static File getCacheDir(Context ctx) {
    File file = null;/*from www . j  a  v a  2  s  .c o  m*/
    try {
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED); //sd??
        if (sdCardExist) {
            file = new File(Environment.getExternalStorageDirectory(), VConstant.BASE_DIR);
        } else if (ctx != null) {
            file = ctx.getCacheDir();
        }
    } catch (Exception e) {
        file = new File(Environment.getExternalStorageDirectory(), VConstant.BASE_DIR);
    }

    //
    if (null != file && !file.exists()) {
        if (!file.mkdirs()) {
            throw new RuntimeException("can't make dirs dstravel/acache in " + file.getAbsolutePath());
        }
    }

    return file;
}

From source file:com.nf2m.util.ImageCache.java

/**
 * Get UriObserver 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
 */
@NonNull
static File getDiskCacheDir(@NonNull 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; // you still need a default value if not mounted

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        if (getExternalCacheDir(context) != null) {
            cachePath = getExternalCacheDir(context).getPath(); // most likely your null value
        }
    } else {
        if (context.getCacheDir() != null) {
            cachePath = context.getCacheDir().getPath();
        }
    }

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

From source file:com.javielinux.utils.Utils.java

public static File getFileForSaveURL(Context cnt, String file) {
    return new File(cnt.getCacheDir(), getHashFromFile(file));
}

From source file:com.raptureinvenice.webimageview.cache.WebImageCache.java

public Bitmap getBitmapFromDiskCache(Context context, String urlString, int diskCacheTimeoutInSeconds) {
    if (mIsDiskCachingEnabled) {
        Bitmap bitmap = null;/*from w  w  w .  j a  v a  2  s .c o  m*/
        File path = context.getCacheDir();
        InputStream is = null;
        String hashedURLString = hashURLString(urlString);

        // correct timeout
        if (diskCacheTimeoutInSeconds < 0) {
            diskCacheTimeoutInSeconds = mDefaultDiskCacheTimeoutInSeconds;
        }

        File file = new File(path, hashedURLString);

        if (file.exists() && file.canRead()) {
            // check for timeout
            if ((file.lastModified() + (diskCacheTimeoutInSeconds * 1000L)) < new Date().getTime()) {
                Log.v(TAG, "Expiring disk cache (TO: " + diskCacheTimeoutInSeconds + "s) for URL " + urlString);

                // expire
                file.delete();
            } else {
                try {
                    is = new FileInputStream(file);

                    bitmap = BitmapFactory.decodeStream(is);
                    Log.v(TAG, "Retrieved " + urlString + " from disk cache (TO: " + diskCacheTimeoutInSeconds
                            + "s).");
                } catch (Exception ex) {
                    Log.e(TAG, "Could not retrieve " + urlString + " from disk cache: " + ex.toString());
                } finally {
                    try {
                        is.close();
                    } catch (Exception ex) {
                    }
                }
            }
        }

        return bitmap;
    }

    return null;
}

From source file:org.stenerud.kscrash.KSCrash.java

/** Install the crash reporter.
 *
 * @param context The application context.
 *//* w  ww  . j a v  a  2s  . c o  m*/
public void install(Context context) throws IOException {
    String appName = context.getApplicationInfo().processName;
    File installDir = new File(context.getCacheDir().getAbsolutePath(), "KSCrash");
    install(appName, installDir.getCanonicalPath());

    // TODO: Put this elsewhere
    oldUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            KSCrash.this.reportJavaException(e);
            KSCrash.oldUncaughtExceptionHandler.uncaughtException(t, e);
        }
    });
}

From source file:at.jclehner.appopsxposed.BugReportBuilder.java

public BugReportBuilder(Context context) {
    mContext = context;//from  w w w .j av  a  2 s .co m
    mDeviceId = getDeviceId();
    mReportTime = getReportTime();
    mBugReportDir = new File(context.getCacheDir(), "reports");
    mBugReportFile = new File(mBugReportDir, "report_" + mDeviceId + "_" + mReportTime + ".txt");
}