Example usage for android.content Context getExternalCacheDirs

List of usage examples for android.content Context getExternalCacheDirs

Introduction

In this page you can find the example usage for android.content Context getExternalCacheDirs.

Prototype

public abstract File[] getExternalCacheDirs();

Source Link

Document

Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place cache files it owns.

Usage

From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static File getBestAvailableCacheRoot(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // In KitKat we can query multiple devices.
        // TODO: optimize for stability instead of picking first one
        File[] roots = context.getExternalCacheDirs();
        if (roots != null) {
            for (File root : roots) {
                if (root == null) {
                    continue;
                }/*w  w  w.  ja v a2 s  .co m*/

                if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(root))) {
                    return root;
                }
            }
        }

    } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // Pre-KitKat, only one external storage device was addressable
        return context.getExternalCacheDir();
    }

    // Worst case, resort to internal storage
    return context.getCacheDir();
}