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

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

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

Context.getExternalCacheDir介绍

暂无

代码示例

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

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

代码示例来源:origin: Naoki2015/CircleDemo

@Override
  public File getCacheDirectory() {
    return context.getExternalCacheDir();
  }
}, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE));

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

public static File getParentFile(@NonNull Context context) {
    final File externalSaveDir = context.getExternalCacheDir();
    if (externalSaveDir == null) {
      return context.getCacheDir();
    } else {
      return externalSaveDir;
    }
  }
}

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

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

代码示例来源: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: bumptech/glide

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

代码示例来源:origin: ArthurHub/Android-Image-Cropper

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

代码示例来源:origin: stackoverflow.com

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

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

static String getDiskExternalCacheDir(Context context) {
  File mFile = context.getExternalCacheDir();
  if (Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(mFile))) {
    return mFile.getAbsolutePath();
  }
  return null;
}

代码示例来源:origin: Curzibn/Luban

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

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

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

代码示例来源:origin: stackoverflow.com

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

}

代码示例来源:origin: commonsguy/cw-omnibus

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

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 返回缓存文件夹
 */
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();
  }
}

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

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

代码示例来源:origin: iMeiji/Toutiao

public static void clearAllCache(Context context) {
  deleteDir(context.getCacheDir());
  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    deleteDir(context.getExternalCacheDir());
  }
}

代码示例来源:origin: czy1121/update

@Override
public void update() {
  mApkFile = new File(mContext.getExternalCacheDir(), mInfo.md5 + ".apk");
  if (UpdateUtil.verify(mApkFile, mInfo.md5)) {
    doInstall();
  } else {
    doDownload();
  }
}

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

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

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

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

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

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
}

相关文章

Context类方法