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:cn.devin.common.util.ACache.java

public static ACache get(Context ctx, String cacheName) {
    File f = new File(ctx.getExternalCacheDir(), cacheName);
    return get(f, MAX_SIZE, MAX_COUNT);
}

From source file:cn.devin.common.util.ACache.java

public static ACache get(Context ctx, long max_zise, int max_count) {
    File f = new File(ctx.getExternalCacheDir(), "ACache");
    return get(f, max_zise, max_count);
}

From source file:com.laurencedawson.image_management.ImageManager.java

/**
 * Get the cache directory to store images in
 * @param context The calling {@link Context}
 * @return a {@link File} to save images in
 *//* www  .  j a v  a2 s  .  c  o m*/
private static File getCacheDir(final Context context) {
    if (context != null) {
        // Try to grab a reference to the external cache dir
        final File directory = context.getExternalCacheDir();

        // If the file is OK, return it
        if (directory != null) {
            return directory;
        }

        // If that fails, try to get a reference to the internal cache
        // This is a rare edge case but occasionally users don't have external
        // storage / their SDCard is removed / the folder is corrupt
        else {
            return context.getCacheDir();
        }
    }
    return null;
}

From source file:com.laurencedawson.image_management.ImageManager.java

/**
 * Clear the local image cache of images over n days old
 * @param context The calling {@link Context}
 * @param int Max age of the images in days
 *//*from   w  ww.j a  va 2  s.  co m*/
public static void clearCache(Context context, final int maxDays) {

    // Grab the time now
    long time = new Date().getTime();

    // If we can access the external cache, empty that first
    if (context.getExternalCacheDir() != null) {
        String[] children = context.getExternalCacheDir().list();
        for (int i = children.length - 1; i >= 0; i--) {
            final File file = new File(context.getExternalCacheDir(), children[i]);
            final Date lastModified = new Date(file.lastModified());
            final long difference = time - lastModified.getTime();
            final int days = (int) (difference / (24 * 60 * 60 * 1000));

            if (days >= maxDays) {
                file.delete();
            }
        }
    }

    // If we can access the internal cache, empty that too
    if (context.getCacheDir() != null) {
        String[] children = context.getCacheDir().list();
        for (int i = children.length - 1; i >= 0; i--) {
            final File file = new File(context.getCacheDir(), children[i]);
            final Date lastModified = new Date(file.lastModified());
            final long difference = time - lastModified.getTime();
            final int days = (int) (difference / (24 * 60 * 60 * 1000));

            if (days >= maxDays) {
                file.delete();
            }
        }
    }
}

From source file:com.gelakinetic.mtgfam.helpers.lruCache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context    The context to use//from  ww  w  . ja  v a  2 s. c om
 * @param uniqueName A unique directory name to append to the cache dir
 * @return The cache dir
 */
private static File getDiskCacheDir(Context context, String uniqueName) throws NullPointerException {
    // 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.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? context.getExternalCacheDir().getPath()
                    : context.getCacheDir().getPath();

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

From source file:com.ethanco.simpleframe.utils.ACache.java

/**
 * //from  w  w w .  j av  a2 s . c  o  m
 *
 * @param ctx
 * @param cacheName
 * @return
 */
private static File getDefualtCacheDir(Context ctx, String cacheName) {
    File f;
    if (USE_EXTERNAL) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            f = new File(ctx.getExternalCacheDir(), cacheName);
        } else {
            f = new File(ctx.getCacheDir(), cacheName);
        }
    } else {
        f = new File(ctx.getCacheDir(), cacheName);
    }
    return f;
}

From source file:com.pavlospt.rxfile.RxFile.java

private static File fileFromUri(Context context, Uri data) throws Exception {
    DocumentFile file = DocumentFile.fromSingleUri(context, data);
    String fileType = file.getType();
    String fileName = file.getName();
    File fileCreated;/*  ww  w. ja  v  a2  s  . co m*/
    ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data,
            Constants.READ_MODE);
    InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
    logDebug("External cache dir:" + context.getExternalCacheDir());
    String filePath = context.getExternalCacheDir() + Constants.FOLDER_SEPARATOR + fileName;
    String fileExtension = fileName.substring((fileName.lastIndexOf('.')) + 1);
    String mimeType = getMimeType(fileName);

    logDebug("From Google Drive guessed type: " + getMimeType(fileName));

    logDebug("Extension: " + fileExtension);

    if (fileType.equals(Constants.APPLICATION_PDF) && mimeType == null) {
        filePath += "." + Constants.PDF_EXTENSION;
    }

    if (!createFile(filePath)) {
        return new File(filePath);
    }

    ReadableByteChannel from = Channels.newChannel(inputStream);
    WritableByteChannel to = Channels.newChannel(new FileOutputStream(filePath));
    fastChannelCopy(from, to);
    from.close();
    to.close();
    fileCreated = new File(filePath);
    logDebug("Path for made file: " + fileCreated.getAbsolutePath());
    return fileCreated;
}

From source file:com.xxxifan.devbox.library.rxfile.RxFile.java

private static File fileFromUri(Context context, Uri data) throws Exception {
    DocumentFile file = DocumentFile.fromSingleUri(context, data);
    String fileType = file.getType();
    String fileName = file.getName();
    File fileCreated;//from  w w  w .  ja v  a 2 s.c o m
    ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(data,
            Constants.READ_MODE);
    InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
    Log.e(TAG, "External cache dir:" + context.getExternalCacheDir());
    String filePath = context.getExternalCacheDir() + Constants.FOLDER_SEPARATOR + fileName;
    String fileExtension = fileName.substring((fileName.lastIndexOf('.')) + 1);
    String mimeType = getMimeType(fileName);

    Log.e(TAG, "From Drive guessed type: " + getMimeType(fileName));

    Log.e(TAG, "Extension: " + fileExtension);

    if (fileType.equals(Constants.APPLICATION_PDF) && mimeType == null) {
        filePath += "." + Constants.PDF_EXTENSION;
    }

    if (!createFile(filePath)) {
        return new File(filePath);
    }

    ReadableByteChannel from = Channels.newChannel(inputStream);
    WritableByteChannel to = Channels.newChannel(new FileOutputStream(filePath));
    fastChannelCopy(from, to);
    from.close();
    to.close();
    fileCreated = new File(filePath);
    Log.e(TAG, "Path for made file: " + fileCreated.getAbsolutePath());
    return fileCreated;
}

From source file:com.apptentive.android.sdk.util.Util.java

public static File getDiskCacheDir(Context context) {
    File appCacheDir = null;/*from  ww  w.j a v a2 s  . c o m*/
    if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable())
            && hasPermission(context, "android.permission.WRITE_EXTERNAL_STORAGE")) {
        appCacheDir = context.getExternalCacheDir();
    }

    if (appCacheDir == null && context != null) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:com.aps490.drdc.prototype.MainActivity.java

public static void initNativeLib(Context context) {
    try {/*from  w  w w. j a  va2s.c o  m*/
        // Try loading our native lib, see if it works...
        System.loadLibrary("libarchitect");
    } catch (UnsatisfiedLinkError er) {
        ApplicationInfo appInfo = context.getApplicationInfo();
        String libName = "libarchitect.so";
        String destPath = context.getFilesDir().toString();
        try {
            String soName = destPath + File.separator + libName;
            new File(soName).delete();
            UnzipUtil.extractFile(appInfo.sourceDir, "lib/" + Build.CPU_ABI + "/" + libName, destPath);
            System.load(soName);
        } catch (IOException e) {
            // extractFile to app files dir did not work. Not enough space? Try elsewhere...
            destPath = context.getExternalCacheDir().toString();
            // Note: location on external memory is not secure, everyone can read/write it...
            // However we extract from a "secure" place (our apk) and instantly load it,
            // on each start of the app, this should make it safer.
            String soName = destPath + File.separator + libName;
            new File(soName).delete(); // this copy could be old, or altered by an attack
            try {
                UnzipUtil.extractFile(appInfo.sourceDir, "lib/" + Build.CPU_ABI + "/" + libName, destPath);
                System.load(soName);
            } catch (IOException e2) {
                Log.e("AMMAR:", "Exception in InstallInfo.init(): " + e);
                e.printStackTrace();
            }
        }
    }
}