List of usage examples for android.content Context getCacheDir
public abstract File getCacheDir();
From source file:Main.java
public static File getCacheDir(Context context) { return isExternalStorageWritable() ? context.getExternalCacheDir() : context.getCacheDir(); }
From source file:Main.java
/** * Enable HTTP response cache, available from Android 4.0 onward, using reflection. *///from www. j a va 2s.c o m static void enableHttpResponseCache(Context context, long cacheSize) { try { File httpCacheDir = new File(context.getCacheDir(), "http"); Class.forName("android.net.http.HttpResponseCache").getMethod("install", File.class, long.class) .invoke(null, httpCacheDir, cacheSize); } catch (Exception httpResponseCacheNotAvailable) { // Do nothing, user is using a version prior to 4.0 } }
From source file:Main.java
public static File saveStreamToCacheFile(Context context, InputStream inputStream, String name) { String FilePath = context.getCacheDir().getAbsolutePath() + name; return saveStreamToFile(inputStream, FilePath); }
From source file:Main.java
public static File extractAssetToFile(Context context, String file) { File cacheFile = new File(context.getCacheDir(), file); try {//w w w . j a v a2 s. c o m InputStream inputStream = context.getAssets().open(file); 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) { e.printStackTrace(); return null; } return cacheFile; }
From source file:Main.java
public static void saveBitmapToCacheDir(Context context, Bitmap bitmap, String name) { try {//w w w . ja v a2 s .co m File file = new File(context.getCacheDir(), name); if (file.exists()) { file.delete(); } FileOutputStream fos = context.openFileOutput(name, MODE_PRIVATE); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void save2Cache(Context context, String cover, Bitmap bitmap) { File file = new File(context.getCacheDir(), cover + ".png"); FileOutputStream fileOutputStream = null; try {//ww w . jav a2 s .com fileOutputStream = new FileOutputStream(file); if (bitmap != null) { if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) { fileOutputStream.flush(); } } } catch (FileNotFoundException e) { file.delete(); e.printStackTrace(); } catch (IOException e) { file.delete(); e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static File getFile(Context context, Bitmap sourceImg) { try {// ww w .j a va2s .c o m File f = new File(context.getCacheDir(), System.currentTimeMillis() + "temp.jpg"); f.createNewFile(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int options = 100; sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos); while (bos.toByteArray().length / 1024 > 100) { bos.reset(); options -= 10; sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos); } byte[] bitmapdata = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close(); return f; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static File getCacheDir(Context context, String dirName) { File file = null;/*from www. j a v a2 s . c o m*/ if (dirName == null) { file = context.getCacheDir(); } else { file = new File(context.getCacheDir(), dirName); } if (!file.exists()) { file.mkdirs(); } return file; }
From source file:Main.java
public static File fileFromAsset(Context context, String assetName) throws IOException { File outFile = new File(context.getCacheDir(), assetName + "-pdfview.pdf"); copy(context.getAssets().open(assetName), outFile); return outFile; }
From source file:Main.java
public static File getCacheDir(Context c) { File cacheDir = c.getExternalCacheDir(); if (null == cacheDir) cacheDir = c.getCacheDir(); if (!cacheDir.exists()) cacheDir.mkdirs();// w w w. j av a 2s . c o m return cacheDir; }