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.umeng.comm.ui.widgets.ImageBrowser.java

private String getCacheDir() throws IOException {
    Context context = getContext();
    String cachePath;//  ww w.j ava2 s .c o  m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        Log.d("", "### context : " + context + ", dir = " + context.getExternalCacheDir());
        cachePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    File cacheFile = new File(cachePath + File.separator + DeviceUtils.getAppName(context));
    if (!cacheFile.exists()) {
        cacheFile.mkdir();
    }
    return cacheFile.getAbsolutePath();
}

From source file:de.unwesen.web.DownloadCache.java

private String getCacheDir() {
    Context ctx = mContext.get();
    if (null == ctx) {
        return null;
    }/*from  w ww  .  jav a 2 s . co  m*/

    File cache_dir = ctx.getCacheDir();
    String root = cache_dir.getPath();

    if (mTunables.useExternalStorage()
            && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        cache_dir = Environment.getExternalStorageDirectory();
        root = cache_dir.getPath() + File.separator + "data" + File.separator + ctx.getPackageName();
    }

    // Log.d(LTAG, "Cache dir: " + root + File.separator + mCachePrefix + File.separator);
    return root + File.separator + mCachePrefix + File.separator;
}

From source file:com.gh4a.utils.HttpImageGetter.java

public HttpImageGetter(Context context) {
    mContext = context;/*w w  w  .  j a v  a 2  s  . c  om*/
    mCacheDir = context.getCacheDir();

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    final Point size = new Point();

    wm.getDefaultDisplay().getSize(size);
    mWidth = size.x;
    mHeight = size.y;

    mLoadingDrawable = ContextCompat.getDrawable(context,
            UiUtils.resolveDrawable(context, R.attr.loadingPictureIcon));
    mLoadingDrawable.setBounds(0, 0, mLoadingDrawable.getIntrinsicWidth(),
            mLoadingDrawable.getIntrinsicHeight());

    mErrorDrawable = ContextCompat.getDrawable(context,
            UiUtils.resolveDrawable(context, R.attr.contentPictureIcon));
    mErrorDrawable.setBounds(0, 0, mErrorDrawable.getIntrinsicWidth(), mErrorDrawable.getIntrinsicHeight());
}

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

/**
 * Clear the local image cache of images over n days old
 * @param context The calling {@link Context}
 * @param int Max age of the images in days
 *///from w  w w.  j ava 2s . c  o m
public static void clearCache(Context context, final int maxDays) {

    // Grab the time now
    long time = new Date().getTime();

    // If we can access the external cache, empty that first
    if (context.getExternalCacheDir() != null) {
        String[] children = context.getExternalCacheDir().list();
        for (int i = children.length - 1; i >= 0; i--) {
            final File file = new File(context.getExternalCacheDir(), children[i]);
            final Date lastModified = new Date(file.lastModified());
            final long difference = time - lastModified.getTime();
            final int days = (int) (difference / (24 * 60 * 60 * 1000));

            if (days >= maxDays) {
                file.delete();
            }
        }
    }

    // If we can access the internal cache, empty that too
    if (context.getCacheDir() != null) {
        String[] children = context.getCacheDir().list();
        for (int i = children.length - 1; i >= 0; i--) {
            final File file = new File(context.getCacheDir(), children[i]);
            final Date lastModified = new Date(file.lastModified());
            final long difference = time - lastModified.getTime();
            final int days = (int) (difference / (24 * 60 * 60 * 1000));

            if (days >= maxDays) {
                file.delete();
            }
        }
    }
}

From source file:com.phonegap.customcamera.NativeCameraLauncher.java

private String getTempDirectoryPath(Context ctx) {
    File cache = null;//from   w  w w .  j a  va 2s  . co m

    // SD Card Mounted
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
                + ctx.getPackageName() + "/cache/");
    }
    // Use internal storage
    else {
        cache = ctx.getCacheDir();
    }

    // Create the cache directory if it doesn't exist
    if (!cache.exists()) {
        cache.mkdirs();
    }

    return cache.getAbsolutePath();
}

From source file:com.trk.aboutme.facebook.internal.FileLruCache.java

public FileLruCache(Context context, String tag, Limits limits) {
    this.tag = tag;
    this.limits = limits;
    this.directory = new File(context.getCacheDir(), tag);
    this.lock = new Object();

    // Ensure the cache dir exists
    this.directory.mkdirs();

    // Remove any stale partially-written files from a previous run
    BufferFile.deleteAll(this.directory);
}

From source file:com.zivacare.android.sdk.ZivaCareConfig.java

/**
 * Constructor for ZivaCareConfig/*from   w w  w  .  j av  a  2s .c  o m*/
 *
 * @param context - context of the app
 * @param demo    - if enabled then the get request will be called with
 *                access_token=demo
 */
public ZivaCareConfig(@NonNull Context context, boolean demo) {
    this.demo = demo;
    readConfig(context);

    this.cacheFile = new File(context.getCacheDir().getPath() + "/" + "ziva_cache");
    if (cacheFile != null && cacheFile.exists()) {
        try {
            String cacheStr = readCacheFile();
            if (cacheStr != null && !cacheStr.trim().isEmpty()) {
                setAccessTokenFromResponse(cacheStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.apache.cordova.filesystemroots.FileSystemRoots.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    Activity activity = cordova.getActivity();
    Context context = activity.getApplicationContext();

    availableFilesystems = new HashMap<String, String>();
    availableFilesystems.put("files", context.getFilesDir().getAbsolutePath());
    availableFilesystems.put("files-external", context.getExternalFilesDir(null).getAbsolutePath());
    availableFilesystems.put("documents", new File(context.getFilesDir(), "Documents").getAbsolutePath());
    availableFilesystems.put("sdcard", Environment.getExternalStorageDirectory().getAbsolutePath());
    availableFilesystems.put("cache", context.getCacheDir().getAbsolutePath());
    availableFilesystems.put("cache-external", context.getExternalCacheDir().getAbsolutePath());
    availableFilesystems.put("root", "/");

    installedFilesystems = new HashSet<String>();

    String filesystemsStr = activity.getIntent().getStringExtra("androidextrafilesystems");
    if (filesystemsStr == null) {
        filesystemsStr = "files,files-external,documents,sdcard,cache,cache-external";
    }//  w  w w. j  a  v a 2 s  .  c  o  m

    String[] filesystems = filesystemsStr.split(",");

    FileUtils filePlugin = (FileUtils) webView.pluginManager.getPlugin("File");
    if (filePlugin != null) {
        /* Register filesystems in order */
        for (String fsName : filesystems) {
            if (!installedFilesystems.contains(fsName)) {
                String fsRoot = availableFilesystems.get(fsName);
                if (fsRoot != null) {
                    File newRoot = new File(fsRoot);
                    if (newRoot.mkdirs() || newRoot.isDirectory()) {
                        filePlugin.registerFilesystem(new LocalFilesystem(fsName, cordova, fsRoot));
                        installedFilesystems.add(fsName);
                    } else {
                        Log.d(TAG, "Unable to create root dir for fileystem \"" + fsName + "\", skipping");
                    }
                } else {
                    Log.d(TAG, "Unrecognized extra filesystem identifier: " + fsName);
                }
            }
        }
    } else {
        Log.w(TAG, "File plugin not found; cannot initialize file-system-roots plugin");
    }

}

From source file:com.facebook.internal.FileLruCache.java

public FileLruCache(Context context, String tag, Limits limits) {
    this.tag = tag;
    this.limits = limits;
    this.directory = new File(context.getCacheDir(), tag);
    this.lock = new Object();

    // Ensure the cache dir exists
    if (this.directory.mkdirs() || this.directory.isDirectory()) {
        // Remove any stale partially-written files from a previous run
        BufferFile.deleteAll(this.directory);
    }//from  w w w.  ja  va2  s.com
}

From source file:co.mwater.foregroundcameraplugin.ForegroundCameraLauncher.java

/**
 * Determine if we can use the SD Card to store the temporary file. If not
 * then use the internal cache directory.
 * //  w  w  w  . ja  v a 2s .  c o  m
 * @return the absolute path of where to store the file
 */
private String getTempDirectoryPath(Context ctx) {
    File cache = null;

    // SD Card Mounted
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
                + ctx.getPackageName() + "/cache/");
    }
    // Use internal storage
    else {
        cache = ctx.getCacheDir();
    }

    // Create the cache directory if it doesn't exist
    if (!cache.exists()) {
        cache.mkdirs();
    }

    return cache.getAbsolutePath();
}