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.gecq.musicwave.cache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise)
 *
 * @param context The {@link Context} to use
 * @param uniqueName A unique directory name to append to the cache
 *            directory//from   w w  w.  ja  v  a2s  . co m
 * @return The cache directory
 */
public static final File getDiskCacheDir(final Context context, final String uniqueName) {
    // getExternalCacheDir(context) returns null if external storage is not ready
    final String cachePath = getExternalCacheDir(context) != null ? getExternalCacheDir(context).getPath()
            : context.getCacheDir().getPath();
    return new File(cachePath, uniqueName);
}

From source file:org.mewx.wenku8.global.GlobalConfig.java

public static void initImageLoader(Context context) {
    UnlimitedDiscCache localUnlimitedDiscCache = new UnlimitedDiscCache(
            new File(GlobalConfig.getFirstStoragePath() + "cache"),
            new File(context.getCacheDir() + File.separator + "imgs"));
    DisplayImageOptions localDisplayImageOptions = new DisplayImageOptions.Builder()
            .resetViewBeforeLoading(true).cacheOnDisk(true).cacheInMemory(true)
            .bitmapConfig(Bitmap.Config.RGB_565).resetViewBeforeLoading(true)
            .displayer(new FadeInBitmapDisplayer(250)).build();
    ImageLoaderConfiguration localImageLoaderConfiguration = new ImageLoaderConfiguration.Builder(context)
            .diskCache(localUnlimitedDiscCache).defaultDisplayImageOptions(localDisplayImageOptions).build();
    ImageLoader.getInstance().init(localImageLoaderConfiguration);
}

From source file:image_cache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context    The context to use//from   ww  w  .  j  ava 2s  .co 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

    if (context == null) {
        return null;
    }

    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.appgeneration.magmanager.imagefetcher.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use// w  w w . j a  va  2 s . c  om
 * @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.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable()
                    ? (getExternalCacheDir(context) != null ? getExternalCacheDir(context).getPath()
                            : context.getCacheDir().getPath())
                    : context.getCacheDir().getPath();

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

From source file:com.momock.util.Logger.java

public static void open(Context context, final String name, int maxLogFiles, int level) {
    if (!enabled)
        return;//w ww.j a va2 s  .c  om
    appName = context.getPackageName();
    logName = name;
    logFileName = logName + "[" + new SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(new Date())
            + "].log";
    if (logStream == System.out) {
        File logDir = null;
        try {
            if (getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                logDir = Environment.getExternalStorageDirectory();
            } else if (context != null) {
                logDir = context.getCacheDir();
            }
            if (logDir != null) {
                android.util.Log.d("Logger", logDir.getAbsolutePath());
                String[] fs = logDir.list(new FilenameFilter() {

                    @Override
                    public boolean accept(File dir, String filename) {
                        return filename.startsWith(logName + "[") && filename.endsWith("].log");
                    }

                });
                List<String> allLogs = new ArrayList<String>();
                for (int i = 0; fs != null && i < fs.length; i++)
                    allLogs.add(fs[i]);
                Collections.sort(allLogs);
                for (int i = 0; i < allLogs.size() - maxLogFiles + 1; i++)
                    new File(logDir, allLogs.get(i)).delete();
                logStream = new PrintStream(new FileOutputStream(new File(logDir, logFileName), false));
            }
        } catch (IOException e) {
            android.util.Log.e("Logger", "Fails to create log file!", e);
        }
    }
    logLevel = level;

    logStream.println("========== Logger Begin ==========");
    logStream.flush();
}

From source file:Main.java

protected static Uri scaleDown(Uri imageUri, int targetSize, Context context) {
    System.gc();//from   w  w  w .j  av  a 2 s  .c om
    String imagePath = getImagePath(imageUri, context);
    Bitmap currentImage = BitmapFactory.decodeFile(imagePath);

    int targetWidth = targetSize;
    int targetHeight = targetSize;

    int width = currentImage.getWidth();
    int height = currentImage.getHeight();

    if (width < targetWidth || height < targetHeight) {
        currentImage.recycle();
        currentImage = null;
        System.gc();
        return Uri.parse(imageUri.toString());
    }

    height = (int) (height * (float) targetWidth / width);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(currentImage, targetWidth, height, false);

    if (currentImage != scaledBitmap) {
        currentImage.recycle();
        currentImage = null;
    }
    System.gc();
    File imageFile;
    try {
        imageFile = new File(context.getCacheDir(), "vumatch-upload-00.jpeg");
        FileOutputStream output;

        output = new FileOutputStream(imageFile);
        boolean result = scaledBitmap.compress(CompressFormat.JPEG, 90, output);
        if (result) {
            scaledBitmap.recycle();
            scaledBitmap = null;
            return Uri.fromFile(imageFile);
        } else {
            return null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.andrew.apollo.cache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise)
 * /*www  .j  a va2  s . co  m*/
 * @param context The {@link Context} to use
 * @param uniqueName A unique directory name to append to the cache
 *            directory
 * @return The cache directory
 */
public static final File getDiskCacheDir(final Context context, final 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:github.daneren2005.dsub.util.FileUtil.java

public static <T extends Serializable> boolean serialize(Context context, T obj, String fileName) {
    Output out = null;//w  w  w  .j  a v  a  2s .co  m
    try {
        RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
        out = new Output(new FileOutputStream(file.getFD()));
        synchronized (kryo) {
            kryo.writeObject(out, obj);
        }
        return true;
    } catch (Throwable x) {
        Log.w(TAG, "Failed to serialize object to " + fileName);
        return false;
    } finally {
        Util.close(out);
    }
}

From source file:github.daneren2005.dsub.util.FileUtil.java

public static <T extends Serializable> boolean serializeCompressed(Context context, T obj, String fileName) {
    Output out = null;/*from  w  w  w. j  a va  2 s .c  om*/
    try {
        RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
        out = new Output(new DeflaterOutputStream(new FileOutputStream(file.getFD())));
        synchronized (kryo) {
            kryo.writeObject(out, obj);
        }
        return true;
    } catch (Throwable x) {
        Log.w(TAG, "Failed to serialize compressed object to " + fileName);
        return false;
    } finally {
        Util.close(out);
    }
}

From source file:com.amachikhin.vkmachikhin.utils.image_cache.ImageCache.java

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context    The context to use//w  ww .j av a  2 s  .  com
 * @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.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            ? getExternalCacheDir(context).getPath()
            : context.getCacheDir().getPath();

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