@Override public File getCacheDirectory() { File cacheDirectory = context.getCacheDir(); if (cacheDirectory == null) { return null; } if (diskCacheName != null) { return new File(cacheDirectory, diskCacheName); } return cacheDirectory; } }, diskCacheSize);
@Nullable private File getInternalCacheDirectory() { File cacheDirectory = context.getCacheDir(); if (cacheDirectory == null) { return null; } if (diskCacheName != null) { return new File(cacheDirectory, diskCacheName); } return cacheDirectory; }
/** Creates a new empty file in the directory returned by {@link Context#getCacheDir()}. */ public static File createTempFile(Context context, String prefix) throws IOException { return File.createTempFile(prefix, null, context.getCacheDir()); }
static File createDefaultCacheDir(Context context) { File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE); if (!cache.exists()) { //noinspection ResultOfMethodCallIgnored cache.mkdirs(); } return cache; }
/** * @param context * @return WebView 的缓存路径 */ public static String getCachePath(Context context) { return context.getCacheDir().getAbsolutePath() + AGENTWEB_CACHE_PATCH; }
/** * 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(); }
@NonNull @Override public File getFile(@NonNull Context context, @NonNull String fileName) { return new File(context.getCacheDir(), fileName); } },
/** * Returns the cache file for the given url if it exists. Checks for both json and zip. * Returns null if neither exist. */ @Nullable private File getCachedFile(String url) throws FileNotFoundException { File jsonFile = new File(appContext.getCacheDir(), filenameForUrl(url, FileExtension.Json, false)); if (jsonFile.exists()) { return jsonFile; } File zipFile = new File(appContext.getCacheDir(), filenameForUrl(url, FileExtension.Zip, false)); if (zipFile.exists()) { return zipFile; } return null; }
/** * Returns a directory with the given name in the private cache directory of the application to * use to store retrieved media and thumbnails. * * @param context A context. * @param cacheName The name of the subdirectory in which to store the cache. * @see #getPhotoCacheDir(android.content.Context) */ @Nullable public static File getPhotoCacheDir(@NonNull Context context, @NonNull String cacheName) { File cacheDir = context.getCacheDir(); if (cacheDir != null) { File result = new File(cacheDir, cacheName); if (!result.mkdirs() && (!result.exists() || !result.isDirectory())) { // File wasn't able to create a directory, or the result exists but not a directory return null; } return result; } if (Log.isLoggable(TAG, Log.ERROR)) { Log.e(TAG, "default disk cache dir is null"); } return null; }
private static File markerFile() { if (markerFile == null) { final Context context = FileDownloadHelper.getAppContext(); markerFile = new File(context.getCacheDir() + File.separator + MAKER_FILE_NAME); } return markerFile; }
/** * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system. * * @param context Application context * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images") * @return Cache {@link File directory} */ public static File getOwnCacheDirectory(Context context, String cacheDir) { File appCacheDir = null; if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) { appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir); } if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) { appCacheDir = context.getCacheDir(); } return appCacheDir; }
/** * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system. * * @param context Application context * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images") * @return Cache {@link File directory} */ public static File getOwnCacheDirectory(Context context, String cacheDir, boolean preferExternal) { File appCacheDir = null; if (preferExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) { appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir); } if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) { appCacheDir = context.getCacheDir(); } return appCacheDir; }
static void clearCache(final Context context, final int numDays) { Log.i("Info", String.format("Starting cache prune, deleting files older than %d days", numDays)); int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays); Log.i("Info", String.format("Cache pruning completed, %d files deleted", numDeletedFiles)); }
synchronized static File getAttachmentsDirectory() { if (attachmentsDirectory == null) { attachmentsDirectory = new File( FacebookSdk.getApplicationContext().getCacheDir(), ATTACHMENTS_DIR_NAME); } return attachmentsDirectory; }
/** * If the file created by {@link #writeTempCacheFile(InputStream, FileExtension)} was successfully parsed, * this should be called to remove the temporary part of its name which will allow it to be a cache hit in the future. */ void renameTempFile(FileExtension extension) { String fileName = filenameForUrl(url, extension, true); File file = new File(appContext.getCacheDir(), fileName); String newFileName = file.getAbsolutePath().replace(".temp", ""); File newFile = new File(newFileName); boolean renamed = file.renameTo(newFile); L.debug("Copying temp file to real file (" + newFile + ")"); if (!renamed) { L.warn( "Unable to rename cache file " + file.getAbsolutePath() + " to " + newFile.getAbsolutePath() + "."); } }
/** * Create image getter for context * * @param context */ @Inject public HttpImageGetter(Context context) { this.context = context; dir = context.getCacheDir(); width = ServiceUtils.getDisplayWidth(context); loading = new LoadingImageGetter(context, R.dimen.image_loading_size); okHttpClient = new OkHttpClient(); }
private File writeVideoToFile() throws IOException { byte[] videoData = loadVideoBytes(); File parent = context.getCacheDir(); if (!parent.mkdirs() && (!parent.exists() || !parent.isDirectory())) { throw new IllegalStateException("Failed to mkdirs for: " + parent); } File toWrite = new File(parent, "temp.jpeg"); if (toWrite.exists() && !toWrite.delete()) { throw new IllegalStateException("Failed to delete existing temp file: " + toWrite); } OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(toWrite)); os.write(videoData); os.close(); } finally { if (os != null) { os.close(); } } return toWrite; }
@Before public void setUp() { context = InstrumentationRegistry.getTargetContext(); cacheDir = context.getCacheDir(); }
@Test public void shouldWriteToCacheDir() throws Exception { assertThat(context.getCacheDir()).isNotNull(); File cacheTest = new File(context.getCacheDir(), "__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 getCacheDir_shouldCreateDirectory() throws Exception { assertThat(context.getCacheDir().exists()).isTrue(); }