Context getExternalCacheDir - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Context getExternalCacheDir method.

This code example shows how to use the following methods:getCacheDir, getExternalCacheDir, getPackageName
	 */ 
	public static File getExternalCacheDir(Context context)
	{ 
		if (AndroidVersionCheckUtils.hasFroyo()) 
		{ 
			return context.getExternalCacheDir();
		} 
		final String cacheDir = "/Android/data/" + context.getPackageName()
				+ "/cache/"; 
		return new File(Environment.getExternalStorageDirectory().getPath()
				+ cacheDir);
	} 
 
	/** 
	 * 检查如果外部存储器是内置的或是可移动的。 
	 *  
	 * @return 如果外部存储是可移动的(就像一个SD卡)返回为 true,否则false。 
	 */ 
	public static boolean isExternalStorageRemovable() 
	{ 

7
CodeRank
This code example shows how to use the following methods:getExternalCacheDir, getPackageName
	 */ 
	@SuppressLint("NewApi") 
	private static String getExternalCacheDir(Context context) {
		// android 2.2 以后才支持的特性 
		if (hasExternalCacheDir()) { 
			return context.getExternalCacheDir().getPath() + File.separator
					+ "request"; 
		} 
 
		// Before Froyo we need to construct the external cache dir ourselves 
		// 2.2以前我们需要自己构造 
		final String cacheDir = "/Android/data/" + context.getPackageName()
				+ "/cache/request/"; 
		return Environment.getExternalStorageDirectory().getPath() + cacheDir;
	} 
 
	private static boolean hasExternalCacheDir() { 
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
	} 
 
7
CodeRank
This code example shows how to use the following methods:getExternalCacheDir
    assertTrue(context.getCacheDir().exists());
  } 
 
  @Test 
  public void getExternalCacheDir_shouldCreateDirectory() throws Exception { 
    assertTrue(context.getExternalCacheDir().exists());
  } 
 
  @Test 
  public void shouldWriteToCacheDir() throws Exception { 
    assertNotNull(context.getCacheDir());
    File cacheTest = new File(context.getCacheDir(), "__test__");
 
    assertThat(cacheTest.getAbsolutePath()).startsWith(System.getProperty("java.io.tmpdir"));
    assertThat(cacheTest.getAbsolutePath()).contains("android-cache");
    assertThat(cacheTest.getAbsolutePath()).endsWith(File.separator + "__test__");
 
    FileOutputStream fos = null;
    try { 
      fos = new FileOutputStream(cacheTest);
6
CodeRank
This code example shows how to use the following methods:getExternalCacheDir
//	    final File extCacheDir = new File(Environment.getExternalStorageDirectory(), 
//	        "/Android/data/" + context.getApplicationInfo().packageName + "/cache/"); 
//	    extCacheDir.mkdirs(); 
//	    return extCacheDir; 
 
        return context.getExternalCacheDir();
    } 
 
    @TargetApi(11) 
    public static void recreate(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            activity.recreate();
        } 
    } 
 
    @TargetApi(11) 
    public static void setLayerType(View view, int layerType, Paint paint) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            view.setLayerType(layerType, paint);
        } 
6
CodeRank
This code example shows how to use the following methods:getExternalCacheDir, getPackageName
     * @return The external cache dir 
     */ 
    @SuppressLint("NewApi") 
    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);
    } 
 
    /** 
     * Check how much usable space is available at a given path. 
     *  
     * @param path The path to check 
     * @return The space available in bytes 
     */ 
    @SuppressLint("NewApi") 
6
CodeRank
This code example shows how to use the following methods:getExternalCacheDir, getPackageName
	 * @return The external cache dir 
	 */ 
	@SuppressLint("NewApi") 
	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);
	} 
 
	/** 
	 * Check how much usable space is available at a given path. 
	 *  
	 * @param path 
	 *            The path to check 
6
CodeRank
This code example shows how to use the following methods:getExternalCacheDir
 
	@TargetApi(Build.VERSION_CODES.FROYO)
	private static class GetExternalCacheDirAccessorFroyo { 
 
		private static File getExternalCacheDir(final Context context) {
			return context.getExternalCacheDir();
		} 
	} 
 
	@TargetApi(Build.VERSION_CODES.FROYO)
	private static class GetExternalStoragePublicDirectoryAccessorFroyo { 
		public static File getExternalStoragePublicDirectory(final String type) {
			return Environment.getExternalStoragePublicDirectory(type);
		} 
	} 
} 
6
CodeRank
This code example shows how to use the following methods:getExternalCacheDir
     * chooses to capture a photo to be attached . We pass {#link@Uri} to the Camera app, 
     * which streams the captured image to the uri. Internally we write the media content 
     * to this file. It's named '.temp.jpg' so Gallery won't pick it up. 
     */ 
    public static String getScrapPath(Context context, String fileName) {
        return context.getExternalCacheDir().getAbsolutePath() + "/" + fileName;
    } 
 
    public static String getScrapPath(Context context) {
        return getScrapPath(context, ".temp.jpg");
    } 
 
    /** 
     * renameScrapFile renames the single scrap file to a new name so newer uses of the scrap 
     * file won't overwrite the previously captured data. 
     * @param fileExtension file extension for the temp file, typically ".jpg" or ".3gp" 
     * @param uniqueIdentifier a separator to add to the file to make it unique, 
     *        such as the slide number. This parameter can be empty or null. 
     * @return uri of renamed file. If there's an error renaming, null will be returned 
     */ 
6
CodeRank

Related Context getExternalCacheDir Questions & Answers:

getExternalCacheDir Method Overview
Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Environment.getExternalStorageDirectory() where the application can place cache files it owns.
See Code Examples for other Android Context Methods: