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.scanvine.android.util.SVDownloadManager.java

public static void DeleteOldCacheFiles(Context context) {
    try {//w ww .j a  v a  2s . c o m
        File cacheDir = context.getCacheDir();
        for (File file : cacheDir.listFiles()) {
            if (System.currentTimeMillis() - file.lastModified() > 1000 * 60 * 60 * 24 * 7) {
                Log.i("SVDownloadManager", "Deleting old cache file " + file);
                file.delete();
            }
        }
    } catch (Exception ex) {
        Log.e("SVDownloadManager", "Error deleting old cache files");
    }
}

From source file:Main.java

/**
 * @param context//from   ww  w .  j a  v  a2 s  . c om
 * @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

public static File getDiskCacheDir(Context context, String uniqueName) {
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            ? getExternalCacheDir(context).getPath()
            : context.getCacheDir().getPath();

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

From source file:Main.java

public static String dstPath(Context context) {
    String cachePath;//from  w ww.j  av a 2  s.c  o m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return cachePath;
}

From source file:Main.java

public static void writeBitmapToDisk(String url, Bitmap bmp, Context ctx, CompressFormat format) {
    FileOutputStream fos;//from   w  w  w.  ja  v a2s.com
    String fileName = constructFileName(url);
    try {
        if (bmp != null) {
            fos = new FileOutputStream(new File(ctx.getCacheDir(), fileName));
            bmp.compress(format, 75, fos);
            fos.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates temporary file in external storage, or in case when extrnal storage is not available
 * temporary file is created in the internal storage.
 * @param context - Context/* w w w  . java  2s .c om*/
 * @return instance of java.io.File or null if error occured.
 */
public static File createTempFile(Context context) {
    if (context == null)
        throw new IllegalArgumentException();

    File saveToDir = context.getExternalCacheDir();

    if (saveToDir == null) {
        saveToDir = context.getCacheDir();
    }

    File tempFile = null;

    try {
        tempFile = File.createTempFile("parrot", "", saveToDir);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return tempFile;
}

From source file:Main.java

private static File createMediaFile(Context context, String parentPath) {
    String state = Environment.getExternalStorageState();
    File rootDir = state.equals(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory()
            : context.getCacheDir();

    File folderDir = new File(rootDir.getAbsolutePath() + parentPath);
    if (!folderDir.exists() && folderDir.mkdirs()) {

    }// www .j  ava 2 s  . c  om

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
    String fileName = APP_NAME + "_" + timeStamp + "";
    File tmpFile = new File(folderDir, fileName + POSTFIX);
    return tmpFile;
}

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   w  w  w . j a v  a2  s. co m
        return context.getCacheDir();
    }
}

From source file:com.example.android.tvleanback.recommendation.RecommendationBuilder.java

private static File getNotificationBackground(Context context, int notificationId) {
    return new File(context.getCacheDir(), "tmp" + Integer.toString(notificationId) + ".png");
}

From source file:org.messic.android.smarttv.activities.recommendations.RecommendationBuilder.java

private static File getNotificationBackground(Context context, long notificationId) {
    return new File(context.getCacheDir(), "tmp" + Long.toString(notificationId) + ".png");
}