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 getRobotCacheFile(Context context, String assetname) throws IOException {
    File cacheFile = new File(context.getCacheDir(), assetname);
    try {//from www .ja v  a 2 s .c o m
        InputStream inputStream = context.getAssets().open(assetname);
        try {
            FileOutputStream outputStream = new FileOutputStream(cacheFile);
            try {
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, len);
                }
            } finally {
                outputStream.close();
            }
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        throw new IOException("Could not open " + assetname, e);
    }
    return cacheFile;
}

From source file:org.michaelevans.etsyblur.utils.Utils.java

private static File getCacheDir(Context context, String dirName) {
    return new File(context.getCacheDir(), dirName);
}

From source file:org.samcrow.ridgesurvey.Storage.java

private static File pathForResource(Context ctx, int resid) {
    return new File(ctx.getCacheDir().getAbsolutePath(), resid + ".resource");
}

From source file:Main.java

public static File getPhoneCacheDir(Context context) {
    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
    return new File(context.getCacheDir().getPath() + cacheDir);
}

From source file:Main.java

public static File getTempFile(Context context, String fileName) {
    File file = null;//from  w  w w .  j a  v a  2  s  .c o  m
    try {
        file = File.createTempFile(fileName, null, context.getCacheDir());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:Main.java

public static File getTempStorageDirectory(Context context, String additionalStorageFolder) {
    String storageSubDir = Strings.nullToEmpty(additionalStorageFolder);
    return new File(context.getCacheDir(), storageSubDir);
}

From source file:Main.java

/**
 * Create temp file. If has external storage create in external else create
 * in internal.//from w ww  . jav  a 2 s .c o m
 *
 * @param context
 * @return
 * @throws IOException
 */
public static File createTempFile(Context context) throws IOException {
    if (!hasExternalStorage()) {
        return createTempFile(context, context.getCacheDir());
    } else {
        return createTempFile(context, context.getExternalFilesDir("caches"));
    }
}

From source file:Main.java

public static void WriteTxtFile(Context context, String strcontent) {

    String strContent = strcontent + "\n";
    try {/*from w  w w.j a  v a  2s .  c  o  m*/
        File file = new File(context.getCacheDir() + File.separator + "wanjia.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(file.length());
        raf.write(strContent.getBytes());
        raf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * clean internal cache(/data/data/com.xxx.xxx/cache)
 *
 * @param context/*ww w .  ja v  a  2  s. co  m*/
 */
public static void cleanInternalCache(Context context) {
    deleteFilesInDirectory(context.getCacheDir());
}

From source file:com.android.volley.toolbox.Volley.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 HttpStack} to use for the network, or null for default.
 * @return A started {@link RequestQueue} instance.
 *///from   ww w.  j  a  v a 2 s .c  om
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        if (Build.VERSION.SDK_INT >= 9) {
            stack = new HurlStack();
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();

    return queue;
}