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

@SuppressLint("SdCardPath")
public static File getCacheDirectory(Context context, boolean preferExternal, String dirName) {
    File appCacheDir = null;/*from  w w  w.j  a  v a2 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/";
        Log.w("Can't define system cache directory! '%s' will be used.", cacheDirPath);
        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:Main.java

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())
            || !Environment.isExternalStorageRemovable() ? Environment.getExternalStorageDirectory().getPath()
                    : context.getCacheDir().getPath();
    return new File(cachePath + File.separator + uniqueName);
}

From source file:tech.zhiqu.cache.AndroidCache.java

public static AndroidCache getInstance(Context ctx, String cacheName) {
    File file = new File(ctx.getCacheDir(), cacheName);
    return getInstance(file, MAX_SIZE, MAX_COUNT);
}

From source file:com.owncloud.android.lib.testclient.TestActivity.java

/**
 * Extracts file from AssetManager to cache folder.
 * /*from   w  w w .  j av  a 2 s.  c om*/
 * @param   fileName   Name of the asset file to extract.
 * @param   context      Android context to access assets and file system.
 * @return            File instance of the extracted file.
 */
public static File extractAsset(String fileName, Context context) throws IOException {
    File extractedFile = new File(context.getCacheDir() + File.separator + fileName);
    if (!extractedFile.exists()) {
        InputStream in = null;
        FileOutputStream out = null;
        in = context.getAssets().open(fileName);
        out = new FileOutputStream(extractedFile);
        byte[] buffer = new byte[BUFFER_SIZE];
        int readCount;
        while ((readCount = in.read(buffer)) != -1) {
            out.write(buffer, 0, readCount);
        }
        out.flush();
        out.close();
        in.close();
    }
    return extractedFile;
}

From source file:com.oscarsalguero.smartystreetsautocomplete.history.DefaultAutocompleteHistoryManager.java

public static AutocompleteHistoryManager fromPath(@NonNull Context context, @NonNull String historyFileName) {
    if (TextUtils.isEmpty(historyFileName)) {
        throw new IllegalArgumentException("Cannot have an empty historyFile name");
    }//www  . j  a  v  a  2  s .co  m

    File historyDir = new File(context.getCacheDir(), BASE_AUTOCOMPLETE_HISTORY_DIR);

    if (!historyDir.exists()) {
        historyDir.mkdirs();
    }

    return new DefaultAutocompleteHistoryManager(new File(historyDir, historyFileName),
            JsonParserResolver.JSON_PARSER);
}

From source file:org.opensilk.video.util.Utils.java

public static File getCacheDir(Context context, String path) {
    File dir = context.getExternalCacheDir();
    if (dir == null || !dir.exists() || !dir.canWrite()) {
        dir = context.getCacheDir();
    }// w  w w . jav a 2 s .c om
    return new File(dir, path);
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;/* w ww . j ava  2s.  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();
    }
    File file = new File(cachePath + File.separator + uniqueName);
    if (!file.exists() && !file.isDirectory()) {
        file.mkdir();
    }
    return file;
}

From source file:Main.java

public static File createCacheDir(Context context, String dirName) {
    File preparedDir;//from   w w  w .  j a  va 2 s .  c  o  m
    if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
        preparedDir = context.getDir(dirName /* + UUID.randomUUID().toString()*/, Context.MODE_PRIVATE);
        Log.i(TAG, "Cache dir initialized at SD card " + preparedDir.getAbsolutePath());
    } else {
        preparedDir = context.getCacheDir();
        Log.i(TAG, "Cache dir initialized at phone storage " + preparedDir.getAbsolutePath());
    }
    if (!preparedDir.exists()) {
        Log.i(TAG, "Cache dir not existed, creating");
        preparedDir.mkdirs();
    }
    return preparedDir;
}

From source file:tech.zhiqu.cache.AndroidCache.java

public static AndroidCache getInstance(Context ctx, long max_size, int max_count) {
    File f = new File(ctx.getCacheDir(), DEFAULT_NAME);
    return getInstance(f, max_size, max_count);
}

From source file:com.artwl.update.StorageUtils.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 and app has appropriate permission. Else -
 * Android defines cache directory on device's file system.
 *
 * @param context Application context/*  w  ww . j  av  a2s .c om*/
 * @return Cache {@link File directory}
 */
public static File getCacheDirectory(Context context, boolean checkExternal) {
    File appCacheDir = null;
    if (checkExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        Log.w(TAG, "Can't define system cache directory! The app should be re-installed.");
    }
    return appCacheDir;
}