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:com.momock.util.Logger.java

@TargetApi(Build.VERSION_CODES.FROYO)
static File getExternalCacheDir(final Context context) {
    return context.getExternalCacheDir();
}

From source file:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

public static void storeSnapshot(List<WifiReading> readings, String fileName, Context c) {

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the external storage
        try {//from  w ww .  j a  v  a 2s.  c o  m
            JSONArray json = readingsToJson(readings);
            File root = c.getExternalCacheDir();
            File jsonFile = new File(root, constructFileName(fileName));
            FileOutputStream out;
            out = new FileOutputStream(jsonFile);
            out.write(json.toString().getBytes());
            out.close();
            U.showToast(constructFileName(fileName));
            Log.d(TAG, "Successfully stored snapshot to SDCard");
        } catch (Exception e) {
            U.showToast("Could not save the snapshot on the SDCard.");
            Log.e(TAG, "Could not save the snapshot on the SDCard.", e);
        }
    } else {
        U.showToast("Cannot write to external storage.");
    }
}

From source file:ch.ethz.inf.vs.android.g54.a4.util.SnapshotCache.java

/**
 * Returns snapshot at index modulo the length of the list, if index is < 0, it returns a random snapshot
 */// w ww.j  a  va 2 s . com
private static List<WifiReading> getSnapshot(int index, Context c) {
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can at least read the external storage
        File root = c.getExternalCacheDir();
        File[] snapshotFiles = root.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String filename) {
                if (filename.endsWith(FILE_EXTENSION)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        if (snapshotFiles.length > 0) {
            if (index < 0) {
                Random rand = new Random();
                index = rand.nextInt(snapshotFiles.length);
            } else {
                index = index % snapshotFiles.length;
            }
            try {
                // read file into a string
                FileInputStream fstream = new FileInputStream(snapshotFiles[index]);
                Log.d(TAG, snapshotFiles[index].getName());
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String fileContents = "";
                String strLine;
                while ((strLine = br.readLine()) != null) {
                    fileContents += strLine;
                }

                // make a json array out of the string
                JSONArray json = new JSONArray(fileContents);

                // parse the json array
                return jsonToReadings(json);
            } catch (Exception e) {
                Log.e(TAG, "Could not read file.");
                return null;
            }
        } else {
            // there are no cached snapshots
            return null;
        }
    } else {
        // we cannot read the external storage
        return null;
    }
}

From source file:org.lol.reddit.common.General.java

public static File getBestCacheDir(final Context context) {

    final File externalCacheDir = context.getExternalCacheDir();

    if (externalCacheDir != null) {
        return externalCacheDir;
    }//from  w  ww  .  j a  v a2s. c o  m

    return context.getCacheDir();
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Get available cache directory/*from  w  w  w  .ja va  2  s  .  c o  m*/
 *
 * @param context a {@link Context}
 * @return a {@link File} pointing to a the external or internal cache
 * directory
 */
public static File getCacheDir(Context context) {
    File path = context.getExternalCacheDir();
    if (path == null) {
        path = context.getCacheDir();
    }
    return path;
}

From source file:org.inaturalist.android.GuideXML.java

/**
 * Creates the root directory that will hold all offline guides
 * @param context/*from w  w w  .  j  a  v a  2s . c om*/
 */
public static void createOfflineGuidesDirectory(Context context) {
    File offlineGuidesDir = new File(context.getExternalCacheDir() + OFFLINE_GUIDE_PATH);
    offlineGuidesDir.mkdirs();
}

From source file:com.ryan.ryanreader.common.General.java

/**
 * /*  w  ww.j  a  v  a  2  s .c o  m*/
 * 
 * @param context
 * @return
 */
public static File getBestCacheDir(final Context context) {

    // SD
    final File externalCacheDir = context.getExternalCacheDir();

    if (externalCacheDir != null) {
        return externalCacheDir;
    }

    return context.getCacheDir();
}

From source file:id.wibisana.priadimulia.imagecaching.cache.ImageCache.java

@TargetApi(8)
public static File getExternalCacheDir(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        return context.getExternalCacheDir();
    }/*from   w  w w.jav a  2 s.  c om*/

    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
    return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}

From source file:com.lxh.util.image.OtherUtils.java

/**
 * ??/*from w w w.  j  ava  2  s  .c o m*/
 * 
 * <pre>
 * SDCard
 * 
 * </pre>
 * 
 * @param context android.content.Context
 * @param dirName ???
 * @return APPapp_cache_path/dirName
 */
public static String getDiskCacheDir(Context context, String dirName) {
    String cachePath = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File externalCacheDir = context.getExternalCacheDir();
        if (externalCacheDir != null) {
            cachePath = externalCacheDir.getPath();
        }
    }
    if (cachePath == null) {
        File cacheDir = context.getCacheDir();
        if (cacheDir != null && cacheDir.exists()) {
            cachePath = cacheDir.getPath();
        }
    }
    return cachePath + File.separator + dirName;
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.java

public static File getCacheDir(Context mContext) {
    File dir = mContext.getExternalCacheDir();
    if (!dir.exists())
        dir.mkdirs();/*from  w ww. ja v  a 2 s.  c o  m*/
    return dir;
}