Example usage for android.content Context getExternalCacheDir

List of usage examples for android.content Context getExternalCacheDir

Introduction

In this page you can find the example usage for android.content Context getExternalCacheDir.

Prototype

@Nullable
public abstract File getExternalCacheDir();

Source Link

Document

Returns absolute path to application-specific directory on the primary shared/external storage device where the application can place cache files it owns.

Usage

From source file:Main.java

public static String getDiskCacheDir(Context context) {

    String cachePath = null;/*  w w w .  j  av  a  2s .  c  o  m*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            && !Environment.isExternalStorageRemovable() && context.getExternalCacheDir() != null) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    return cachePath;
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;//w ww.  ja  v a 2s  . c  o  m
    if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) && context.getExternalCacheDir() != null
            && !TextUtils.isEmpty(context.getExternalCacheDir().getPath())) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);
}

From source file:Main.java

/**
 * @param context//from   w w w.  jav  a  2s .  c  om
 * @param dirName Only the folder name, not full path.
 * @return app_cache_path/dirName
 */
public static String getDiskCacheDir(Context context, String dirName) {
    String cachePath = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File externalCacheDir = context.getExternalCacheDir();
        if (externalCacheDir != null) {
            cachePath = externalCacheDir.getPath();
        }
    }
    if (cachePath == null) {
        File cacheDir = context.getCacheDir();
        if (cacheDir != null && cacheDir.exists()) {
            cachePath = cacheDir.getPath();
        }
    }

    return cachePath + File.separator + dirName;
}

From source file:Main.java

/**
 * Helper method to initiate cache directory. It will return the cache directory in File format,
 * or NULL if the directory path is invalid or not accessible.
 *//*from w w  w.  j  a  v  a  2  s . c o m*/
public static File getCacheDirectory(final Context context, final String path) {
    File cacheDir = null;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {
            cacheDir = context.getExternalCacheDir();
        } catch (NullPointerException e) {
            // Fallback to use internal storage if external storage isn't available.
        }
    }
    if (cacheDir == null) {
        cacheDir = context.getCacheDir();
    }
    return (cacheDir != null && path != null) ? new File(cacheDir, path) : null;
}

From source file:org.catnut.plugin.zhihu.Zhihu.java

/**
 * ??// w w w .  jav a2s  . c  om
 *
 * @param context
 * @param uri
 * @return
 */
public static Uri getCacheImageLocation(Context context, Uri uri) {
    File img = new File(context.getExternalCacheDir() + File.separator + CACHE_IMAGE_LOCATION + File.separator
            + uri.getLastPathSegment());
    return Uri.fromFile(img);
}

From source file:Main.java

public static File getCacheFile(@NonNull Context context, @NonNull String fileName) {
    File savedir = null;/* w  ww.  j  a va  2  s.c  o  m*/
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        savedir = new File(context.getExternalCacheDir(), fileName);
    }

    if (savedir == null) {
        savedir = new File(context.getCacheDir(), fileName);
    }

    if (!savedir.exists()) {
        savedir.mkdirs();
    }
    return savedir;
}

From source file:Main.java

public static String getCachePath(Context context, String uniqueName) {
    String cachePath;/*w w w . j  av  a2 s  .c o m*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return cachePath + File.separator + uniqueName;
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String fileName) {
    String cachePath;//www. j av a2s. co  m
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
            && !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    Log.d("bonus", "cachePath = " + cachePath);
    return new File(cachePath + File.separator + fileName);
}

From source file:Main.java

public static File getAppCacheFile(Context context) {
    File externalCacheDir;//from   www.j  av a 2 s  . c o  m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        externalCacheDir = context.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir == null) {
            externalCacheDir = context.getCacheDir();
        }
    } else {
        externalCacheDir = context.getCacheDir();
    }
    return externalCacheDir;
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;/*ww w.  j a v a2 s  .co  m*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);
}