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.umeng.comm.ui.widgets.ImageBrowser.java

private String getCacheDir() throws IOException {
    Context context = getContext();
    String cachePath;//from  www .  j a v  a 2 s.com
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        Log.d("", "### context : " + context + ", dir = " + context.getExternalCacheDir());
        cachePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    File cacheFile = new File(cachePath + File.separator + DeviceUtils.getAppName(context));
    if (!cacheFile.exists()) {
        cacheFile.mkdir();
    }
    return cacheFile.getAbsolutePath();
}

From source file:com.lillicoder.newsblurry.util.FileLogger.java

/**
 * Gets a {@link File} object for the given filename. If the indicated
 * file already exists, it will be overwritten by a new file. The returned file 
 * lives in this app's external cache directory by default (when external
 * media is not available, the internal cache is used instead).
 * @param filename Filename of the file to get.
 * @return {@link File} object for the given filename.
 *///from   ww  w.j  a  v a  2 s . c om
private File getLogFile(String filename) {
    Assert.assertTrue(!TextUtils.isEmpty(filename));

    // Use cache directory so that files are removed on uninstall.
    Context context = this.getContext();

    // Check for external media availability. If available,
    // write to that directory. Otherwise get the internal cache
    // directory and write there.
    File rootDirectory;
    if (this.isExternalStorageAvailable())
        rootDirectory = context.getExternalCacheDir();
    else
        rootDirectory = context.getCacheDir();

    // If the file to write already exists, delete it.
    File logFile = new File(rootDirectory, filename);
    if (logFile.exists())
        logFile.delete();

    return logFile;
}

From source file:csh.cryptonite.Cryptonite.java

public static File getExternalCacheDir(final Context context) {
    /* Api >= 8 */
    return context.getExternalCacheDir();

    /* Api < 8//w  w  w  . j a  va2 s .  c om
    // e.g. "<sdcard>/Android/data/<package_name>/cache/"
    final File extCacheDir = new File(Environment.getExternalStorageDirectory(),
                                  "/Android/data/" + context.getApplicationInfo().packageName + "/cache/");
    extCacheDir.mkdirs();
    return extCacheDir;*/
}

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

/**
 * Initialize the GuideXML class with the offline (downloaded) version of it
 * @param context the app context/*from w  w w  .j  ava 2 s  . c om*/
 * @param guideId the guide identifier
 */
public GuideXML(Context context, String guideId) {
    // Initialize the class with the offline guide downloaded XML file
    this(context, guideId,
            context.getExternalCacheDir() + OFFLINE_GUIDE_PATH + guideId + "/" + guideId + ".xml");
}

From source file:de.appplant.cordova.emailcomposer.EmailComposerImpl.java

/**
 * Cleans the attachment folder./*  w  ww. ja v a  2s .  c o m*/
 *
 * @param ctx
 * The application context.
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
public void cleanupAttachmentFolder(Context ctx) {
    try {
        File dir = new File(ctx.getExternalCacheDir() + ATTACHMENT_FOLDER);

        if (!dir.isDirectory())
            return;

        File[] files = dir.listFiles();

        for (File file : files) {
            file.delete();
        }
    } catch (Exception npe) {
        Log.w("EmailComposer", "Missing external cache dir");
    }
}

From source file:de.appplant.cordova.emailcomposer.EmailComposerImpl.java

/**
 * The URI for an asset.//from   w w  w  . j  a v  a 2s .  com
 *
 * @param path
 * The given asset path.
 * @param ctx
 * The application context.
 * @return
 * The URI pointing to the given path.
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
private Uri getUriForAssetPath(String path, Context ctx) {
    String resPath = path.replaceFirst("file:/", "www");
    String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
    File dir = ctx.getExternalCacheDir();

    if (dir == null) {
        Log.e("EmailComposer", "Missing external cache dir");
        return Uri.EMPTY;
    }

    String storage = dir.toString() + ATTACHMENT_FOLDER;
    File file = new File(storage, fileName);

    new File(storage).mkdir();

    try {
        AssetManager assets = ctx.getAssets();

        FileOutputStream outStream = new FileOutputStream(file);
        InputStream inputStream = assets.open(resPath);

        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        Log.e("EmailComposer", "File not found: assets/" + resPath);
        e.printStackTrace();
    }

    return Uri.fromFile(file);
}

From source file:de.appplant.cordova.emailcomposer.EmailComposerImpl.java

/**
 * The URI for a base64 encoded content.
 *
 * @param content/*ww  w. j a v a 2 s.  co  m*/
 * The given base64 encoded content.
 * @param ctx
 * The application context.
 * @return
 * The URI including the given content.
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
private Uri getUriForBase64Content(String content, Context ctx) {
    String resName = content.substring(content.indexOf(":") + 1, content.indexOf("//"));
    String resData = content.substring(content.indexOf("//") + 2);
    File dir = ctx.getExternalCacheDir();
    byte[] bytes;

    try {
        bytes = Base64.decode(resData, 0);
    } catch (Exception ignored) {
        Log.e("EmailComposer", "Invalid Base64 string");
        return Uri.EMPTY;
    }

    if (dir == null) {
        Log.e("EmailComposer", "Missing external cache dir");
        return Uri.EMPTY;
    }

    String storage = dir.toString() + ATTACHMENT_FOLDER;
    File file = new File(storage, resName);

    new File(storage).mkdir();

    try {
        FileOutputStream outStream = new FileOutputStream(file);

        outStream.write(bytes);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return Uri.fromFile(file);
}

From source file:org.apache.cordova.filesystemroots.FileSystemRoots.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    Activity activity = cordova.getActivity();
    Context context = activity.getApplicationContext();

    availableFilesystems = new HashMap<String, String>();
    availableFilesystems.put("files", context.getFilesDir().getAbsolutePath());
    availableFilesystems.put("files-external", context.getExternalFilesDir(null).getAbsolutePath());
    availableFilesystems.put("documents", new File(context.getFilesDir(), "Documents").getAbsolutePath());
    availableFilesystems.put("sdcard", Environment.getExternalStorageDirectory().getAbsolutePath());
    availableFilesystems.put("cache", context.getCacheDir().getAbsolutePath());
    availableFilesystems.put("cache-external", context.getExternalCacheDir().getAbsolutePath());
    availableFilesystems.put("root", "/");

    installedFilesystems = new HashSet<String>();

    String filesystemsStr = activity.getIntent().getStringExtra("androidextrafilesystems");
    if (filesystemsStr == null) {
        filesystemsStr = "files,files-external,documents,sdcard,cache,cache-external";
    }//from ww w .j  av  a 2  s  . com

    String[] filesystems = filesystemsStr.split(",");

    FileUtils filePlugin = (FileUtils) webView.pluginManager.getPlugin("File");
    if (filePlugin != null) {
        /* Register filesystems in order */
        for (String fsName : filesystems) {
            if (!installedFilesystems.contains(fsName)) {
                String fsRoot = availableFilesystems.get(fsName);
                if (fsRoot != null) {
                    File newRoot = new File(fsRoot);
                    if (newRoot.mkdirs() || newRoot.isDirectory()) {
                        filePlugin.registerFilesystem(new LocalFilesystem(fsName, cordova, fsRoot));
                        installedFilesystems.add(fsName);
                    } else {
                        Log.d(TAG, "Unable to create root dir for fileystem \"" + fsName + "\", skipping");
                    }
                } else {
                    Log.d(TAG, "Unrecognized extra filesystem identifier: " + fsName);
                }
            }
        }
    } else {
        Log.w(TAG, "File plugin not found; cannot initialize file-system-roots plugin");
    }

}

From source file:de.appplant.cordova.emailcomposer.EmailComposerImpl.java

/**
 * The URI for a resource./*from   w ww  . ja v a2 s. c  o  m*/
 *
 * @param path
 * The given relative path.
 * @param ctx
 * The application context.
 * @return
 * The URI pointing to the given path
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
private Uri getUriForResourcePath(String path, Context ctx) {
    String resPath = path.replaceFirst("res://", "");
    String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
    String resName = fileName.substring(0, fileName.lastIndexOf('.'));
    String extension = resPath.substring(resPath.lastIndexOf('.'));
    File dir = ctx.getExternalCacheDir();

    if (dir == null) {
        Log.e("EmailComposer", "Missing external cache dir");
        return Uri.EMPTY;
    }

    String storage = dir.toString() + ATTACHMENT_FOLDER;
    int resId = getResId(resPath, ctx);
    File file = new File(storage, resName + extension);

    if (resId == 0) {
        Log.e("EmailComposer", "File not found: " + resPath);
    }

    new File(storage).mkdir();

    try {
        Resources res = ctx.getResources();
        FileOutputStream outStream = new FileOutputStream(file);
        InputStream inputStream = res.openRawResource(resId);

        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return Uri.fromFile(file);
}

From source file:csh.cryptonite.Cryptonite.java

public static void setupReadDirs(boolean external, Context context) {
    if (DirectorySettings.INSTANCE.openDir != null) {
        Cryptonite.deleteDir(DirectorySettings.INSTANCE.openDir);
    }//w w  w .ja  v a  2s.c  o m
    if (DirectorySettings.INSTANCE.readDir != null) {
        Cryptonite.deleteDir(DirectorySettings.INSTANCE.readDir);
    }

    if (external && Cryptonite.externalStorageIsWritable()) {
        context.getExternalCacheDir().mkdirs();
        DirectorySettings.INSTANCE.openDir = new File(
                context.getExternalCacheDir().getPath() + "/" + DirectorySettings.OPENPNT);
        DirectorySettings.INSTANCE.readDir = new File(
                context.getExternalCacheDir().getPath() + "/" + DirectorySettings.READPNT);
        DirectorySettings.INSTANCE.openDir.mkdirs();
        DirectorySettings.INSTANCE.readDir.mkdirs();
    } else {
        DirectorySettings.INSTANCE.openDir = context.getDir(DirectorySettings.OPENPNT, Context.MODE_PRIVATE);
        DirectorySettings.INSTANCE.readDir = context.getDir(DirectorySettings.READPNT,
                Context.MODE_WORLD_READABLE);
    }
}