Get the external cache directory - Android Hardware

Android examples for Hardware:Directory

Description

Get the external cache directory

Demo Code


//package com.book2s;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import java.io.File;

public class Main {
    /**/* www  .j  a  va2s. c om*/
     * Get the external cache directory
     * @param context
     * @return
     */
    public static File getExternalCacheDir(final Context context) {
        if (hasExternalCacheDir())
            return context.getExternalCacheDir();

        //Before the Froyo we have to construct the external cache dir by ourself
        final String cacheDir = "/Android/data/" + context.getPackageName()
                + "/cache/";
        return new File(Environment.getExternalStorageDirectory().getPath()
                + cacheDir);
    }

    /**
     * Detect whether the SDK version larger than Froyo
     * @return
     */
    private static boolean hasExternalCacheDir() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO
                && Environment.getExternalStorageState().equals(
                        Environment.MEDIA_MOUNTED);
    }
}

Related Tutorials