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:com.pixby.texo.images.ImageUtil.java

private static String getAppTempPath(Context context) {
    return context.getExternalCacheDir().getPath();
}

From source file:Main.java

/**
 * clean external cache(/mnt/sdcard/android/data/com.xxx.xxx/cache)
 *
 * @param context//from w ww .  j a va2  s .  c om
 */
public static void cleanExternalCache(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        deleteFilesInDirectory(context.getExternalCacheDir());
    }
}

From source file:Main.java

/**
 * @param context/*from w  w  w . j a  v  a2  s.  c  o m*/
 * @param dirName Only the folder name, not contain full path.
 * @return app_cache_path/dirName
 */
public static String getDiskCacheDir(Context context, String dirName) {
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            ? context.getExternalCacheDir().getPath()
            : context.getCacheDir().getPath();

    return cachePath + File.separator + dirName;
}

From source file:Main.java

private static Uri getUriFromAsset(Context mContext, String path) {
    File dir = mContext.getExternalCacheDir();

    if (dir == null) {
        Log.e(TAG, "Missing external cache dir");
        return Uri.EMPTY;
    }//  www.jav  a 2  s . c  o  m
    String resPath = path.replaceFirst("file:/", "www");
    String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
    String storage = dir.toString() + STORAGE_FOLDER;

    if (fileName == null || fileName.isEmpty()) {
        Log.e(TAG, "Filename is missing");
        return Uri.EMPTY;
    }

    File file = new File(storage, fileName);
    FileOutputStream outStream = null;
    InputStream inputStream = null;

    try {
        File fileStorage = new File(storage);
        if (!fileStorage.mkdir())
            Log.e(TAG, "Storage directory could not be created: " + storage);

        AssetManager assets = mContext.getAssets();
        outStream = new FileOutputStream(file);
        inputStream = assets.open(resPath);

        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
        return Uri.fromFile(file);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found: assets/" + resPath);
    } catch (IOException ioe) {
        Log.e(TAG, "IOException occured");
    } catch (SecurityException secEx) {
        Log.e(TAG, "SecurityException: directory creation denied");
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException ioe) {
            Log.e(TAG, "IOException occured while closing/flushing streams");
        }
    }
    return Uri.EMPTY;
}

From source file:Main.java

public static void clearAllCache(Context context) {
    deleteDir(context.getCacheDir());/*ww w. j av  a 2s . c  o m*/
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        deleteDir(context.getExternalCacheDir());
    }
}

From source file:Main.java

public static File getDiskCacheDir(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        return context.getExternalCacheDir();
    } else {//from   ww w .j a  va2s.  c  o m
        return context.getCacheDir();
    }
}

From source file:Main.java

@SuppressWarnings("WeakerAccess")
public static File getAppCacheDir(Context context) {
    File appCacheDir = null;/*from  ww  w . j  av a 2s .  c  om*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        appCacheDir = context.getExternalCacheDir();
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:Main.java

public static String getRootFilePath(Context context) {
    if (hasSDCard()) {
        //return Environment.getExternalStorageDirectory().getAbsolutePath();// filePath:/sdcard/android
        return context.getExternalCacheDir().getAbsolutePath();// filePath:/sdcard/android/+packageName+/cache
    } else {/*from ww w  .  j  ava 2s  . c o m*/
        return context.getCacheDir().getAbsolutePath(); // filePath: /data/data/
    }
}

From source file:Main.java

public static File getCacheDirectory(Context context) {
    if (context == null)
        return null;

    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        return context.getExternalCacheDir();
    }//  ww w .ja  v a 2s . co  m

    return context.getCacheDir();
}

From source file:Main.java

public static String createRootPath(Context context) {
    String cacheRootPath = "";
    if (isSdCardAvailable()) {
        // /sdcard/Android/data/<application package>/cache
        cacheRootPath = context.getExternalCacheDir().getPath();
    } else {//from   w  w  w  .j av a2  s.  c  o m
        // /data/data/<application package>/cache
        cacheRootPath = context.getCacheDir().getPath();
    }
    return cacheRootPath;
}