Android How to - Get Disk Cache Directory








Question

We would like to know how to get Disk Cache Directory.

Answer

/*from w  ww.  j  av  a2 s . c om*/
import java.io.File;

import android.content.Context;
import android.os.Environment;

public class Main {
    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);
    }
    public static File getExternalCacheDir(Context context) {
        final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
        return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
    }
}