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.android.messaging.datamodel.MediaScratchFileProvider.java

private static File getDirectory(final Context context) {
    return new File(context.getCacheDir(), MEDIA_SCRATCH_SPACE_DIR);
}

From source file:Main.java

private static String getTempFilename(Context context) throws IOException {
    File outputDir = context.getCacheDir();
    File outputFile = File.createTempFile("image", "tmp", outputDir);
    return outputFile.getAbsolutePath();
}

From source file:Main.java

public static String getGlideDefaultPath(Context context) {
    if (context == null) {
        throw new RuntimeException("context can't be null");
    }/*from   w ww  . j av a 2s .  co  m*/
    String path = context.getCacheDir().getAbsolutePath();
    if (isSDCard()) {
        String directory = Environment.getExternalStorageDirectory() + "/GankLy/cache/img";
        File file = new File(directory);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return directory;
            }
        }
    }
    return path;
}

From source file:com.gistandard.androidbase.http.extension.VolleyEx.java

/**
 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
 *
 * @param context A {@link Context} to use for creating the cache dir.
 * @param stack An {@link HttpStack} to use for the network, or null for default.
 * @return A started {@link RequestQueue} instance.
 *//*from  ww  w  .  ja v a2 s.com*/
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        if (Build.VERSION.SDK_INT >= 9) {
            try {
                // allow all ssl connection
                HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                stack = new HurlStack(null, new SSLSocketFactoryEx());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();

    return queue;
}

From source file:Main.java

/**
 * Get APNG working directory/*  w ww.  ja  v a  2 s . co m*/
 * @param context Application Context
 * @return Reference to the working directory
 */
public static File getWorkingDir(Context context) {
    File workingDir = null;
    File cacheDir = context.getExternalCacheDir();

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

    if (cacheDir != null) {
        workingDir = new File(String.format("%s/apng/.nomedia/", cacheDir.getPath()));

        if (!workingDir.exists()) {
            workingDir.mkdirs();
        }
    }

    return workingDir;
}

From source file:jp.egg.android.request.volley.EggVolley.java

/**
 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
 *
 * @param context A {@link Context} to use for creating the cache dir.
 * @param stack An {@link HttpStack} to use for the network, or null for default.
 * @return A started {@link RequestQueue} instance.
 *//*  w w  w  .  j a va  2  s.com*/
protected static RequestQueue newRequestQueue(Context context, HttpStack stack, int cacheSizeInBytes) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        if (Build.VERSION.SDK_INT >= 9) {
            stack = new HurlStack();
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir, cacheSizeInBytes), network);
    queue.start();

    return queue;
}

From source file:org.phoneremotecontrol.app.http.HttpServerService.java

public static File getRootDir(Context context) {
    return new File(context.getCacheDir() + "/" + ROOT_DIR_SUFFIX);
}

From source file:Main.java

public static Bitmap getBitmapFromDisk(String url, Context ctx) {

    Bitmap defautBitmap = null;/* w w w . ja  va 2s.c o  m*/
    try {
        String filename = constructFileName(url);
        File filePath = new File(ctx.getCacheDir(), filename);

        if (filePath.exists() && filePath.isFile() && !filePath.isDirectory()) {
            FileInputStream fi;
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inPreferredConfig = Config.RGB_565;
            fi = new FileInputStream(filePath);
            defautBitmap = BitmapFactory.decodeStream(fi, null, opts);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (Exception e) {

    } catch (OutOfMemoryError e) {

    }

    return defautBitmap;
}

From source file:org.proninyaroslav.libretorrent.core.utils.FileIOUtils.java

public static void cleanTempDirectory(Context context) {
    try {//from  w ww  .j a v  a2  s . co m
        FileUtils.cleanDirectory(context.getCacheDir());

    } catch (IOException e) {
        /* Ignore */
    }
}

From source file:Main.java

/**
 * Get a usable cache directory (external if available, internal otherwise)
 *
 * @param context The {@link android.content.Context} to use
 * @param uniqueName A unique directory name to append to the cache
 *            directory/*  w w w.ja  v  a2  s. c o m*/
 * @return The cache directory
 */
public static File getCacheDir(final Context context, final String uniqueName) {
    File cachePath = context.getExternalCacheDir();
    if (cachePath == null || !cachePath.canWrite()) {
        cachePath = context.getCacheDir();
    }
    File cacheDir = new File(cachePath, uniqueName);
    if (!cacheDir.exists()) {
        cacheDir.mkdirs();
    }
    if (!cacheDir.canWrite()) {
        cacheDir.setWritable(true);
    }
    return cacheDir;
}