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:Main.java

public static File getCacheDirectory(Context context) {
    if (context == null)
        return null;

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        return context.getExternalCacheDir();
    }//from   w  w w. j a  v  a  2 s  .  co m

    return context.getCacheDir();
}

From source file:Main.java

/**
 * Get ideal cache directory based on available
 *
 * @return Ideal file location for caching
 */// ww w. j ava  2 s . c  o m
public static File getIdealCacheDirectory(Context context) {
    File externalCacheDir = context.getExternalCacheDir();
    if (getTotalExternalMemorySize() < getTotalInternalMemorySize() || externalCacheDir == null) {
        return context.getCacheDir();
    }
    return externalCacheDir;
}

From source file:net.survivalpad.android.MainActivity.java

private static void copyFromAsset(Context context) {
    if (!BuildConfig.DEBUG && context.getCacheDir().list().length > 0) {
        return;/* w w w  .ja  va2 s . c  o m*/
    }

    AssetManager am = context.getAssets();
    String[] files = null;
    try {
        files = am.list("");
    } catch (IOException e) {
    }

    if (files == null) {
        return;
    }

    for (String file : files) {
        Log.d(TAG, "file = " + file);
        File to = new File(context.getCacheDir(), file);

        try {
            FileUtils.copy(am.open(file), to);
        } catch (IOException e) {
        }

    }
}

From source file:org.droidparts.http.worker.HttpURLConnectionWorker.java

public static void setHttpResponseCacheEnabled(Context ctx, boolean enabled) {
    File cacheDir = new File(ctx.getCacheDir(), "http");
    long cacheSize = 10 * 1024 * 1024; // 10 MiB
    try {//from  w w  w .  j  a  v a 2  s.  c o m
        Class<?> cls = Class.forName("android.net.http.HttpResponseCache");
        if (enabled) {
            cls.getMethod("install", File.class, long.class).invoke(null, cacheDir, cacheSize);
        } else {
            Object instance = cls.getMethod("getInstalled").invoke(null);
            if (instance != null) {
                cls.getMethod("delete").invoke(instance);
            }
        }
    } catch (Exception e) {
        L.i(e);
    }
}

From source file:com.parse.ParseKeyValueCache.java

static void initialize(Context context) {
    initialize(new File(context.getCacheDir(), DIR_NAME));
}

From source file:Main.java

public static void saveThumb(Context context, String thumbName, Bitmap image) {
    if (thumbName == null || thumbName.length() <= 0)
        return;// ww  w.j  a  v  a  2  s . com

    File pictureFile = getOutputMediaFile(context, thumbName);
    File dir = new File(context.getCacheDir() + "/thumbnails");
    if (dir.exists())//too many caches
    {
        File files[] = dir.listFiles();
        if (files.length > CACHE_LIMIT)
            files[0].deleteOnExit();
    }
    if (pictureFile == null) {
        Log.d(TAG, "Error creating media file, check storage permissions: ");// e.getMessage());
        return;
    }
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }
}

From source file:Main.java

@SuppressWarnings("WeakerAccess")
public static File getAppCacheDir(Context context) {
    File appCacheDir = null;//from  w w  w  . ja  v  a2  s .  c  om
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        appCacheDir = context.getExternalCacheDir();
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:com.exampleka.jksonlib.JacksonNetwork.java

/**
 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
 *
 * @param context A {@link Context} to use for creating the cache dir.
 * @param stack An {@link com.android.volley.toolbox.HttpStack} to use for handling network calls
 * @return A started {@link RequestQueue} instance.
 *//*w w w .  ja va2 s  . c  o m*/
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    RequestQueue queue = new RequestQueue(new DiskBasedCache(new File(context.getCacheDir(), "volley")),
            new JacksonNetwork(stack));
    queue.start();
    return queue;
}

From source file:Main.java

/**
 * Returns application cache directory. Cache directory will be created on SD card
 * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted. Else - Android defines cache directory on
 * device's file system./*from  ww w  . ja va 2  s.  c o m*/
 * 
 * @param context Application context
 * @return Cache {@link File directory}
 */
public static File getCacheDirectory(Context context) {
    File appCacheDir = null;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        appCacheDir = getExternalCacheDir(context);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:org.kontalk.service.ServerListUpdater.java

/** The path to the locally cached downloaded server list. */
private static File getCachedListFile(Context context) {
    return new File(context.getCacheDir(), "serverlist.properties");
}