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

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;//w  w w.j av a2  s . c o  m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageEmulated()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    return new File(cachePath + File.separator + uniqueName);
}

From source file:Main.java

public static String getAppCachePath(Context context) {
    String cachePath;//from ww w .j  a v a  2s  . c o  m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        File externalCacheDir = context.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir == null) {
            cachePath = context.getCacheDir().getPath();
        } else {
            cachePath = externalCacheDir.getPath();
        }
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return cachePath;
}

From source file:Main.java

/**
 * Get disk cache directory//from w w w.  ja v a 2 s  .c om
 *
 * @param ctx
 * @param uniqueName Unique name for caching directory, use it separate different types of cached files. eg: bitmap,strings,css,files etc.
 * @return cache directory
 */
public static File getDiskCacheDir(Context ctx, String uniqueName) {

    String cachePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cachePath = ctx.getExternalCacheDir().getPath();
    } else {
        cachePath = ctx.getCacheDir().getPath();
    }

    return new File(cachePath, uniqueName);
}

From source file:Main.java

public static File getCacheDir(Context context) {
    Log.i("getCacheDir", "cache sdcard state: " + Environment.getExternalStorageState());
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File cacheDir = context.getExternalCacheDir();
        if (cacheDir != null && (cacheDir.exists() || cacheDir.mkdirs())) {
            Log.i("getCacheDir", "cache dir: " + cacheDir.getAbsolutePath());
            return cacheDir;
        }/* ww  w.  j  a v  a2s  .c o  m*/
    }

    File cacheDir = context.getCacheDir();
    Log.i("getCacheDir", "cache dir: " + cacheDir.getAbsolutePath());

    return cacheDir;
}

From source file:Main.java

/**
 * Creates the temporary image file in the cache directory.
 *
 * @return The temporary image file.//from w ww.ja v a  2  s.c o m
 * @throws IOException Thrown if there is an error creating the file
 */
static File createTempImageFile(Context context) throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = context.getExternalCacheDir();

    return File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;/*from  w w w .  j av a  2 s.c  om*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        File externalCacheDir = context.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir == null) {
            cachePath = context.getCacheDir().getPath();
        } else {
            cachePath = externalCacheDir.getPath();
        }
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    File file = new File(cachePath + File.separator + uniqueName);
    if (!file.exists() && !file.isDirectory()) {
        file.mkdir();
    }
    return file;
}

From source file:Main.java

private static String storeImage(Context context, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File f = null;/*from w w w  .ja v  a 2 s .  c  o  m*/
    try {
        f = File.createTempFile("citationsImg", ".png", context.getExternalCacheDir());
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return f.getAbsolutePath();
}

From source file:com.mvc.imagepicker.ImagePicker.java

private static File getTemporalFile(Context context) {
    return new File(context.getExternalCacheDir(), TEMP_IMAGE_NAME);
}

From source file:org.opensilk.video.util.Utils.java

public static File getCacheDir(Context context, String path) {
    File dir = context.getExternalCacheDir();
    if (dir == null || !dir.exists() || !dir.canWrite()) {
        dir = context.getCacheDir();/*w  w  w  .  jav a 2s  .c  o  m*/
    }
    return new File(dir, path);
}

From source file:com.bukanir.android.utils.Utils.java

public static boolean isStorageVfat(Context context) {
    try {//from   w w w.  j  av a  2s. co  m
        String cacheDir = context.getExternalCacheDir().toString();
        List<String> items = Arrays.asList(cacheDir.split("/"));
        String path = items.get(1) + "/" + items.get(2);

        String cmd = String.format("/system/bin/mount | grep '%s'", path);
        String[] command = { "/system/bin/sh", "-c", cmd };

        Process process = Runtime.getRuntime().exec(command, null, new File("/system/bin"));
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String line;
        String output = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = in.readLine()) != null) {
            output += line;
        }

        List<String> outputItems = Arrays.asList(output.split(" "));
        if (outputItems.size() >= 3) {
            if (outputItems.get(2).equals("vfat")) {
                return true;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}