@Override public File getCacheDirectory() { File cacheDirectory = context.getExternalCacheDir(); if (cacheDirectory == null) { return null; } if (diskCacheName != null) { return new File(cacheDirectory, diskCacheName); } return cacheDirectory; } }, diskCacheSize);
@Override public File getCacheDirectory() { return context.getExternalCacheDir(); } }, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE));
public static File getParentFile(@NonNull Context context) { final File externalSaveDir = context.getExternalCacheDir(); if (externalSaveDir == null) { return context.getCacheDir(); } else { return externalSaveDir; } } }
@NonNull @Override public File getFile(@NonNull Context context, @NonNull String fileName) { return new File(context.getExternalCacheDir(), fileName); } },
/** * Initialize the StateSaver, usually you want to call this in the Application class * * @param context used to get the available cache dir */ public static void init(Context context) { File externalCacheDir = context.getExternalCacheDir(); if (externalCacheDir != null) cacheDirPath = externalCacheDir.getAbsolutePath(); if (TextUtils.isEmpty(cacheDirPath)) cacheDirPath = context.getCacheDir().getAbsolutePath(); }
@Override public File getCacheDirectory() { File internalCacheDirectory = getInternalCacheDirectory(); // Already used internal cache, so keep using that one, // thus avoiding using both external and internal with transient errors. if ((null != internalCacheDirectory) && internalCacheDirectory.exists()) { return internalCacheDirectory; } File cacheDirectory = context.getExternalCacheDir(); // Shared storage is not available. if ((cacheDirectory == null) || (!cacheDirectory.canWrite())) { return internalCacheDirectory; } if (diskCacheName != null) { return new File(cacheDirectory, diskCacheName); } return cacheDirectory; } }, diskCacheSize);
/** * Get URI to image received from capture by camera. * * @param context used to access Android APIs, like content resolve, it is your * activity/fragment/widget. */ public static Uri getCaptureImageOutputUri(@NonNull Context context) { Uri outputFileUri = null; File getImage = context.getExternalCacheDir(); if (getImage != null) { outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg")); } return outputFileUri; }
public static void saveLogcatToFile(Context context) { String fileName = "logcat_"+System.currentTimeMillis()+".txt"; File outputFile = new File(context.getExternalCacheDir(),fileName); @SuppressWarnings("unused") Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath()); }
static String getDiskExternalCacheDir(Context context) { File mFile = context.getExternalCacheDir(); if (Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(mFile))) { return mFile.getAbsolutePath(); } return null; }
/** * <b>BuildTime:</b> 2014-8-30<br> * <b>Description:</b> Get the external cache directory,it will be bulid a * directory what is name "Android/data/PACKAGE_NAME/cache" for 2.2 system"<br> * * @param context * * @return */ public static File getExternalCacheDir(Context context) { if (hasExternalCacheDir()) { return context.getExternalCacheDir(); } final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir); }
public static String getDefaultSaveRootPath() { if (!TextUtils.isEmpty(defaultSaveRootPath)) { return defaultSaveRootPath; } if (FileDownloadHelper.getAppContext().getExternalCacheDir() == null) { return Environment.getDownloadCacheDirectory().getAbsolutePath(); } else { //noinspection ConstantConditions return FileDownloadHelper.getAppContext().getExternalCacheDir().getAbsolutePath(); } }
public class Utils { public static final int IO_BUFFER_SIZE = 8 * 1024; private Utils() {}; public static boolean isExternalStorageRemovable() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { return Environment.isExternalStorageRemovable(); } return true; } public static File getExternalCacheDir(Context context) { if (hasExternalCacheDir()) { return context.getExternalCacheDir(); } // Before Froyo we need to construct the external cache dir ourselves final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/"; return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir); } public static boolean hasExternalCacheDir() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO; } }
@Override public void onCreate() { super.onCreate(); if (Environment.MEDIA_MOUNTED .equals(Environment.getExternalStorageState())) { String logDirName= "assistlogger_"+ new SimpleDateFormat("yyyyMMdd'-'HHmmss").format(new Date()); logDir= new File(getContext().getExternalCacheDir(), logDirName); logDir.mkdirs(); } }
/** * 返回缓存文件夹 */ public static File getCacheFile(Context context) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = null; file = context.getExternalCacheDir();//获取系统管理的sd卡缓存文件 if (file == null) {//如果获取的文件为空,就使用自己定义的缓存文件夹做缓存路径 file = new File(getCacheFilePath(context)); makeDirs(file); } return file; } else { return context.getCacheDir(); } }
public static String getDefaultSaveRootPath() { if (!TextUtils.isEmpty(defaultSaveRootPath)) { return defaultSaveRootPath; } if (FileDownloadHelper.getAppContext().getExternalCacheDir() == null) { return Environment.getDownloadCacheDirectory().getAbsolutePath(); } else { //noinspection ConstantConditions return FileDownloadHelper.getAppContext().getExternalCacheDir().getAbsolutePath(); } }
public static void clearAllCache(Context context) { deleteDir(context.getCacheDir()); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { deleteDir(context.getExternalCacheDir()); } }
@Override public void update() { mApkFile = new File(mContext.getExternalCacheDir(), mInfo.md5 + ".apk"); if (UpdateUtil.verify(mApkFile, mInfo.md5)) { doInstall(); } else { doDownload(); } }
@Test public void shouldWriteToExternalCacheDir() throws Exception { assertThat(context.getExternalCacheDir()).isNotNull(); File cacheTest = new File(context.getExternalCacheDir(), "__test__"); assertThat(cacheTest.getAbsolutePath()) .startsWith(System.getProperty("java.io.tmpdir")); assertThat(cacheTest.getAbsolutePath()) .endsWith(File.separator + "__test__"); try (FileOutputStream fos = new FileOutputStream(cacheTest)) { fos.write("test".getBytes(UTF_8)); } assertThat(cacheTest.exists()).isTrue(); }
@Test public void getExternalCacheDir_shouldCreateDirectory() throws Exception { assertThat(context.getExternalCacheDir().exists()).isTrue(); }
private static CacheDataSource buildCacheDataSource(Context context, DataSource upstreamSource, boolean useAesEncryption) throws CacheException { File cacheDir = context.getExternalCacheDir(); Cache cache = new SimpleCache(new File(cacheDir, EXO_CACHE_DIR), new NoOpCacheEvictor()); emptyCache(cache); // Source and cipher final String secretKey = "testKey:12345678"; DataSource file = new FileDataSource(); DataSource cacheReadDataSource = useAesEncryption ? new AesCipherDataSource(Util.getUtf8Bytes(secretKey), file) : file; // Sink and cipher CacheDataSink cacheSink = new CacheDataSink(cache, EXO_CACHE_MAX_FILESIZE); byte[] scratch = new byte[3897]; DataSink cacheWriteDataSink = useAesEncryption ? new AesCipherDataSink(Util.getUtf8Bytes(secretKey), cacheSink, scratch) : cacheSink; return new CacheDataSource(cache, upstreamSource, cacheReadDataSource, cacheWriteDataSink, CacheDataSource.FLAG_BLOCK_ON_CACHE, null); // eventListener }