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.ruesga.rview.misc.CacheHelper.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static File getAttachmentCacheDir(Context context) {
    File cacheDir = new File(context.getCacheDir(), ATTACHMENT_CACHE_FOLDER);
    if (!cacheDir.exists()) {
        cacheDir.mkdir();/*from   ww w.  j  a va 2s.co m*/
    }
    return cacheDir;
}

From source file:Main.java

private static boolean CopyFile(Context context, String pathName) {

    boolean isCopyCompleted = false;

    InputStream inputStream = null;
    OutputStream outputStream = null;

    try {/*from  w  w  w . j av a2s .  co  m*/
        inputStream = context.getResources().getAssets().open(pathName);
        File outFile = new File(context.getCacheDir(), pathName);
        if (!outFile.exists()) {
            outFile.createNewFile();
        }

        outputStream = new FileOutputStream(outFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        // read from is to buffer
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
            Log.v("", "data..." + bytesRead);
        }

        isCopyCompleted = true;
        inputStream.close();
        // flush OutputStream to write any buffered data to file
        outputStream.flush();
        outputStream.close();

    } catch (IOException e) {
        isCopyCompleted = false;
        e.printStackTrace();
    }
    return isCopyCompleted;

}

From source file:com.ruesga.rview.misc.CacheHelper.java

public static File getAccountCacheDir(Context context, Account account) {
    return new File(context.getCacheDir(), account.getAccountHash());
}

From source file:Main.java

public static String getDiskCacheDir(Context context) {

    String cachePath = null;/*from  www .j a va 2s  . c o m*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && !Environment.isExternalStorageRemovable() && context.getExternalCacheDir() != null) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    return cachePath;
}

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 and app has appropriate permission. Else -
 * Android defines cache directory on device's file system.
 *
 * @param context Application context//from  w w w  . ja va 2  s. com
 * @return Cache {@link File directory}
 */
public static File getCacheDirectory(Context context) {
    File appCacheDir = null;
    if (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;
}

From source file:com.github.chenxiaolong.dualbootpatcher.patcher.PatcherUtils.java

public synchronized static void extractPatcher(Context context) {
    for (File d : context.getCacheDir().listFiles()) {
        if (d.getName().startsWith("DualBootPatcherAndroid") || d.getName().startsWith("tmp")
                || d.getName().startsWith("data-")) {
            org.apache.commons.io.FileUtils.deleteQuietly(d);
        }//from w w w  .  ja  v  a2  s.c o m
    }
    for (File d : context.getFilesDir().listFiles()) {
        if (d.isDirectory()) {
            for (File t : d.listFiles()) {
                if (t.getName().contains("tmp")) {
                    org.apache.commons.io.FileUtils.deleteQuietly(t);
                }
            }
        }
    }

    File targetFile = getTargetFile(context);
    File targetDir = getTargetDirectory(context);

    if (BuildConfig.BUILD_TYPE.equals("debug") || !targetDir.exists()) {
        try {
            FileUtils.extractAsset(context, sTargetFile, targetFile);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to extract data archive from assets", e);
        }

        // Remove all previous files
        for (File d : context.getFilesDir().listFiles()) {
            org.apache.commons.io.FileUtils.deleteQuietly(d);
        }

        try {
            LibMiscStuff.extractArchive(targetFile.getAbsolutePath(), context.getFilesDir().getAbsolutePath());
        } catch (IOException e) {
            throw new IllegalStateException("Failed to extract data archive", e);
        }

        // Delete archive
        targetFile.delete();
    }
}

From source file:it.restrung.rest.cache.RequestCache.java

/**
 * Gets the cache file based on the requesting context, the cache key and the cache file extension
 *
 * @param context  the requesting context
 * @param cacheKey the cache key//from w  ww  .j  a v  a2s  .  c o m
 * @return the cache file
 */
private static File getCacheFile(Context context, int cacheKey) {
    return new File(String.format("%s/%d%s", context.getCacheDir(), cacheKey, CACHE_EXTENSION));
}

From source file:Main.java

public static File createFile(Context context) {
    File file;/* w  ww.  j a  va 2 s . c om*/
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        String timeStamp = String.valueOf(new Date().getTime());
        file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp + ".jpg");
    } else {
        File cacheDir = context.getCacheDir();
        String timeStamp = String.valueOf(new Date().getTime());
        file = new File(cacheDir, timeStamp + ".jpg");
    }
    return file;
}

From source file:Main.java

public static String getExternalRootDirectory(Context context) {
    if (databaseFolder == null) {
        //The location of the temp internal database used for sync should be
        //in the cache, since it is only needed for <1min at a time, and
        //created every time it is needed
        if (useSAF()) {
            File basePath = new File(
                    context.getCacheDir() + File.separator + "externalDatabaseSync" + File.separator);
            databaseFolder = basePath.getPath();
        } else {/*from   w  ww .  j  ava 2s  .com*/
            return "/storage/usbdisk0/WildRank/cblite";
        }
    }
    return databaseFolder;
}

From source file:de.dreier.mytargets.features.statistics.CsvExporter.java

public static Uri export(Context context, List<Long> roundIds) throws IOException {
    String packageName = context.getPackageName();
    String authority = packageName + ".easyphotopicker.fileprovider";

    final File f = new File(context.getCacheDir(), getExportFileName());
    exportAll(f, roundIds);//from ww  w  . j a v a  2 s .c  om
    return getUriForFile(context, authority, f);
}