Example usage for android.content Context getCacheDir

List of usage examples for android.content Context getCacheDir

Introduction

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

Prototype

public abstract File getCacheDir();

Source Link

Document

Returns the absolute path to the application specific cache directory on the filesystem.

Usage

From source file:com.github.chenxiaolong.dualbootpatcher.settings.AppSharingUtils.java

public static void updateRamdisk(Context context) throws Exception {
    PatcherUtils.extractPatcher(context);

    RomInformation romInfo = RomUtils.getCurrentRom(context);
    if (romInfo == null) {
        throw new Exception("Could not determine current ROM");
    }//from   w  w  w  . j  ava2 s.  c o  m

    String bootImage = String.format(SwitcherUtils.KERNEL_PATH_ROOT, romInfo.id);
    RootFile bootImageFile = new RootFile(bootImage);

    if (!bootImageFile.isFile()) {
        SwitcherUtils.backupKernel(romInfo.id);
    }

    String tmpKernel = context.getCacheDir() + File.separator + "boot.img";
    RootFile tmpKernelFile = new RootFile(tmpKernel);
    bootImageFile.copyTo(tmpKernelFile);
    tmpKernelFile.chmod(0777);

    BootImage bi = new BootImage();
    if (!bi.load(tmpKernel)) {
        PatcherError error = bi.getError();
        throw new Exception("Error code: " + error.getErrorCode());
    }

    CpioFile cpio = new CpioFile();
    if (!cpio.load(bi.getRamdiskImage())) {
        PatcherError error = cpio.getError();
        throw new Exception("Error code: " + error.getErrorCode());
    }

    if (cpio.isExists("mbtool")) {
        cpio.remove("mbtool");
    }

    if (!cpio.addFile(
            PatcherUtils.getTargetDirectory(context) + "/binaries/android/" + Build.CPU_ABI + "/mbtool",
            "mbtool", 0755)) {
        PatcherError error = cpio.getError();
        throw new Exception("Error code: " + error.getErrorCode());
    }

    bi.setRamdiskImage(cpio.createData(true));

    if (!bi.createFile(tmpKernel)) {
        PatcherError error = bi.getError();
        throw new Exception("Error code: " + error.getErrorCode());
    }

    if (bi.isLoki()) {
        String aboot = context.getCacheDir() + File.separator + "aboot.img";
        SwitcherUtils.dd(SwitcherUtils.ABOOT_PARTITION, aboot);
        new RootFile(aboot).chmod(0666);

        String lokiKernel = context.getCacheDir() + File.separator + "kernel.lok";

        if (lokiPatch("boot", aboot, tmpKernel, lokiKernel) != 0) {
            throw new Exception("Failed to loki patch new boot image");
        }

        new File(lokiKernel).delete();

        org.apache.commons.io.FileUtils.moveFile(new File(lokiKernel), new File(tmpKernel));
    }

    // Copy to target
    new RootFile(tmpKernel).copyTo(bootImageFile);
    bootImageFile.chmod(0755);

    SwitcherUtils.writeKernel(romInfo.id);
}

From source file:Main.java

/**
 * Returns application cache directory. Cache directory will be created on SD card
 * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
 * on device's file system depending incoming parameters.
 *
 * @param context        Application context
 * @param preferExternal Whether prefer external location for cache
 * @return Cache {@link File directory}.<br />
 * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
 * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
 */// w  ww .ja  v  a  2  s  .  co  m
public static File getCacheDirectory(Context context, boolean preferExternal) {
    File appCacheDir = null;
    String externalStorageState;
    try {
        externalStorageState = Environment.getExternalStorageState();
    } catch (NullPointerException e) { // (sh)it happens (Issue #660)
        externalStorageState = "";
    } catch (IncompatibleClassChangeError e) { // (sh)it happens too (Issue #989)
        externalStorageState = "";
    }
    if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState) && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}

From source file:com.android.pc.ioc.image.ImageCache.java

/**
 * ?/*from www  .j av a  2  s.com*/
 * 
 * @author gdpancheng@gmail.com 2014-5-19 ?4:05:28
 * @param context
 * @param uniqueName
 * @return File
 */
public static File getDiskCacheDir(Context context, String uniqueName) {
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath()
                    : context.getCacheDir().getPath();

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

From source file:com.example.image.ImageCache.java

/**
 * Get the external app cache directory.
 *
 * @param context The context to use//from  ww w  . j av a  2s  .c  om
 * @return The external cache dir
 */
@TargetApi(8)
public static File getExternalCacheDir(Context context) {
    if (VersionUtils.hasFroyo()) {
        File cacheDir = context.getExternalCacheDir();
        if (cacheDir != null) {
            return cacheDir;
        }
        return context.getCacheDir();
    }

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

From source file:com.hippo.ehviewer.EhApplication.java

@NonNull
public static Conaco<ImageBitmap> getConaco(@NonNull Context context) {
    EhApplication application = ((EhApplication) context.getApplicationContext());
    if (application.mConaco == null) {
        Conaco.Builder<ImageBitmap> builder = new Conaco.Builder<>();
        builder.hasMemoryCache = true;/*from  w ww  . j  a v  a2s.c  om*/
        builder.memoryCacheMaxSize = getMemoryCacheMaxSize();
        builder.hasDiskCache = true;
        builder.diskCacheDir = new File(context.getCacheDir(), "thumb");
        builder.diskCacheMaxSize = 80 * 1024 * 1024; // 80MB
        builder.okHttpClient = getOkHttpClient(context);
        builder.objectHelper = getImageBitmapHelper(context);
        builder.debug = DEBUG_CONACO;
        application.mConaco = builder.build();
    }
    return application.mConaco;
}

From source file:com.cognizant.trumobi.em.Email.java

public static void setTempDirectory(Context context) {
    sTempDirectory = context.getCacheDir();
}

From source file:Main.java

public static File downSample(Context context, Uri uri) throws Exception {
    Bitmap b = null;/*from  w w w.  j  av a  2s.  co m*/

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    int scale = 1;
    if (o.outHeight > MAX_SIZE || o.outWidth > MAX_SIZE) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    InputStream is = context.getContentResolver().openInputStream(uri);
    b = BitmapFactory.decodeStream(is, null, o2);
    is.close();

    File outputDir = context.getCacheDir();
    File outputFile = File.createTempFile("avatar", ".jpg", outputDir);
    FileOutputStream fos = new FileOutputStream(outputFile);
    b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.close();

    return outputFile;
}

From source file:com.manning.androidhacks.hack040.util.DiskLruCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 * // w  w w .j a v  a2 s. c  o  m
 * @param context
 *          The context to use
 * @param uniqueName
 *          A unique directory name to append to the cache dir
 * @return The cache dir
 */
public static File getDiskCacheDir(Context context, String uniqueName) {

    // Check if media is mounted or storage is built-in, if so, try and use
    // external cache dir
    // otherwise use internal cache dir
    final String cachePath = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
            || !Utils.isExternalStorageRemovable() ? Utils.getExternalCacheDir(context).getPath()
                    : context.getCacheDir().getPath();

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

From source file:org.nativescript.widgets.image.Cache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context    The context to use/* w  w  w. ja  va  2 s. c o  m*/
 * @param uniqueName A unique directory name to append to the cache dir
 * @return The cache dir
 */
public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final File cacheFilePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? getExternalCacheDir(context) : context.getCacheDir();
    // In case there is no external storage isExternalStorageRemovable returns False and we get Null from getExternalCacheDir.
    // If this is the case - fall back to internal cache dir.
    final String cachePath = cacheFilePath != null ? cacheFilePath.getPath() : context.getCacheDir().getPath();
    return new File(cachePath + File.separator + uniqueName);
}

From source file:Main.java

/**
 * Returns application cache directory. Cache directory will be created on SD card
 * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
 * on device's file system depending incoming parameters.
 *
 * @param context        Application context
 * @param preferExternal Whether prefer external location for cache
 * @return Cache {@link File directory}.<br />
 * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
 * {@link android.content.Context#getCacheDir() Context.getCacheDir()} returns null).
 *///w w  w . j a  v a2  s  .c o  m
public static File getCacheDirectory(Context context, boolean preferExternal, boolean cache, String customDir) {
    File appCacheDir = null;
    String externalStorageState;
    try {
        externalStorageState = Environment.getExternalStorageState();
    } catch (NullPointerException e) { // (sh)it happens (Issue #660)
        externalStorageState = "";
    } catch (IncompatibleClassChangeError e) { // (sh)it happens too (Issue #989)
        externalStorageState = "";
    }
    if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState) && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context, cache, customDir);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath;
        cacheDirPath = "/data/data/" + context.getPackageName() + "/" + customDir + "/";

        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
}