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

/**
 * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
 * is mounted. Else - Android defines cache directory on device's file system.
 * //from  w  w  w .  j  av  a  2  s .c  o  m
 * @param context Application context
 * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
 * @return Cache {@link File directory}
 */
public static File getOwnCacheDirectory(Context context, String cacheDir) {
    File appCacheDir = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
    }
    if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:com.isaacrf.epicbitmaprenderer.core.EpicBitmapCache.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    Context from where EpicBitmapCache is being used.
 * @param uniqueName Unique name for cache directory.
 * @return {@link File} pointing to cache directory
 *///w  ww .java 2 s  .  co  m
public static File getDiskCacheDir(Context context, String uniqueName) {
    File externalCacheDir = context.getExternalCacheDir();
    File cacheDir = context.getCacheDir();
    String cachePath;

    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) && externalCacheDir != null) {
        cachePath = externalCacheDir.getPath();
    } else {
        cachePath = cacheDir.getPath();
    }

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

From source file:Main.java

public static String getAppCachePath(Context context) {
    String cachePath;/*from   w w w  .  ja  v  a 2 s  .c o  m*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        File externalCacheDir = context.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir == null) {
            cachePath = context.getCacheDir().getPath();
        } else {
            cachePath = externalCacheDir.getPath();
        }
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return cachePath;
}

From source file:com.bt.download.android.util.SystemUtils.java

public static File getCacheDir(Context context, String directory) {
    File cache;//from w  ww .  ja  va  2  s .c  om

    if (isPrimaryExternalStorageMounted()) {
        cache = context.getExternalCacheDir();
    } else {
        cache = context.getCacheDir();
    }

    return new File(cache, directory);
}

From source file:Main.java

public static File getAppCacheFile(Context context) {
    File externalCacheDir;//from w  w  w . ja  v  a 2  s .  co m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        externalCacheDir = context.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir == null) {
            externalCacheDir = context.getCacheDir();
        }
    } else {
        externalCacheDir = context.getCacheDir();
    }
    return externalCacheDir;
}

From source file:Main.java

public static File getOwnCacheDirectory(Context context, String cacheDir) {
    File appCacheDir = null;// w  w w.j  av  a  2 s  . c om
    if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) {
        appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
    }
    if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:at.ac.uniklu.mobile.sportal.util.Utils.java

/**
 * Deletes all content from the application's cache directory.
 * source: http://groups.google.com/group/android-developers/browse_thread/thread/e9eb0a17a3c7c768
 * /*from w ww .  j av  a2 s  .  c o  m*/
 * @param context
 */
public static void clearAppCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {
        // TODO: handle exception 
    }
}

From source file:Main.java

public static File getCacheDirectory(Context context, boolean preferExternal, String dirName) {
    File appCacheDir = null;/*w ww  . ja va  2  s . c o  m*/
    if (preferExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context, dirName);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:Main.java

public static File getCacheDirectory(Context context, boolean preferExternal, String dirName) {
    File appCacheDir = null;//  ww  w  .  j  a  v a 2  s  .  co m
    if (preferExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context, dirName);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
        Log.w("Can't define system cache directory! '%s' will be used.", cacheDirPath);
        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:de.spiritcroc.ownlog.FileHelper.java

public static ImportFiles unpackImport(Context context, InputStream inputStream) {
    File importingDir = new File(context.getCacheDir(), context.getString(R.string.import_file_dir));
    rmdir(importingDir);//  w ww . j a  va2s  . c  o m
    importingDir.mkdirs();
    ZipInputStream in = new ZipInputStream(inputStream);
    ZipEntry entry;
    try {
        while ((entry = in.getNextEntry()) != null) {
            String path = importingDir.getAbsolutePath() + File.separator + entry.getName();
            if (DEBUG)
                Log.d(TAG, "Unzipping path: " + path);
            File f = new File(path);
            if (entry.isDirectory()) {
                if (!f.isDirectory()) {
                    f.mkdirs();
                }
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
                FileOutputStream out = new FileOutputStream(path, false);
                try {
                    int size;
                    while ((size = in.read()) != -1) {
                        out.write(size);
                    }
                    in.closeEntry();
                } finally {
                    try {
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "Import failed", e);
        return null;
    }
    File logDbFile = new File(importingDir, context.getString(R.string.import_file_db));
    File attachmentsDir = new File(importingDir, context.getString(R.string.import_file_attachments));
    if (logDbFile.exists()) {
        return new ImportFiles(logDbFile, attachmentsDir);
    } else {
        Log.w(TAG, "Import failed: database not found");
        return null;
    }
}