android.content.Context.getCacheDir()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(337)

本文整理了Java中android.content.Context.getCacheDir()方法的一些代码示例,展示了Context.getCacheDir()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.getCacheDir()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:getCacheDir

Context.getCacheDir介绍

暂无

代码示例

代码示例来源:origin: bumptech/glide

@Override
 public File getCacheDirectory() {
  File cacheDirectory = context.getCacheDir();
  if (cacheDirectory == null) {
   return null;
  }
  if (diskCacheName != null) {
   return new File(cacheDirectory, diskCacheName);
  }
  return cacheDirectory;
 }
}, diskCacheSize);

代码示例来源:origin: bumptech/glide

@Nullable
private File getInternalCacheDirectory() {
 File cacheDirectory = context.getCacheDir();
 if (cacheDirectory == null) {
  return null;
 }
 if (diskCacheName != null) {
  return new File(cacheDirectory, diskCacheName);
 }
 return cacheDirectory;
}

代码示例来源:origin: google/ExoPlayer

/** 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());
}

代码示例来源:origin: square/picasso

static File createDefaultCacheDir(Context context) {
 File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE);
 if (!cache.exists()) {
  //noinspection ResultOfMethodCallIgnored
  cache.mkdirs();
 }
 return cache;
}

代码示例来源:origin: Justson/AgentWeb

/**
 * @param context
 * @return WebView 的缓存路径
 */
public static String getCachePath(Context context) {
  return context.getCacheDir().getAbsolutePath() + AGENTWEB_CACHE_PATCH;
}

代码示例来源:origin: TeamNewPipe/NewPipe

/**
 * 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();
}

代码示例来源:origin: ACRA/acra

@NonNull
  @Override
  public File getFile(@NonNull Context context, @NonNull String fileName) {
    return new File(context.getCacheDir(), fileName);
  }
},

代码示例来源:origin: airbnb/lottie-android

/**
 * 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;
}

代码示例来源:origin: bumptech/glide

/**
 * 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;
}

代码示例来源:origin: lingochamp/FileDownloader

private static File markerFile() {
  if (markerFile == null) {
    final Context context = FileDownloadHelper.getAppContext();
    markerFile = new File(context.getCacheDir() + File.separator + MAKER_FILE_NAME);
  }
  return markerFile;
}

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

/**
 * 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;
}

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

/**
 * 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;
}

代码示例来源:origin: Justson/AgentWeb

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));
}

代码示例来源:origin: facebook/facebook-android-sdk

synchronized static File getAttachmentsDirectory() {
  if (attachmentsDirectory == null) {
    attachmentsDirectory = new File(
        FacebookSdk.getApplicationContext().getCacheDir(),
        ATTACHMENTS_DIR_NAME);
  }
  return attachmentsDirectory;
}

代码示例来源:origin: airbnb/lottie-android

/**
 * 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() + ".");
 }
}

代码示例来源:origin: pockethub/PocketHub

/**
 * 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();
}

代码示例来源:origin: bumptech/glide

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;
}

代码示例来源:origin: bumptech/glide

@Before
public void setUp() {
 context = InstrumentationRegistry.getTargetContext();
 cacheDir = context.getCacheDir();
}

代码示例来源:origin: robolectric/robolectric

@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();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getCacheDir_shouldCreateDirectory() throws Exception {
 assertThat(context.getCacheDir().exists()).isTrue();
}

相关文章

微信公众号

最新文章

更多

Context类方法