// Pre-KitKat, only one external storage device was addressable
@TargetApi(Build.VERSION_CODES.KITKAT)
    public static File getBestAvailableFilesRoot(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.getExternalFilesDirs(null);
            if (roots != null) {
                for (File root : roots) {
                    if (root == null) {
                        continue;
                    }
                    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.getExternalFilesDir(null);
        }
        // Worst case, resort to internal storage
        return context.getFilesDir();
    }
}
} else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
// Pre-KitKat, only one external storage device was addressable
return context.getExternalFilesDir(null);
}
for (File root : roots) {
if (root == null) {
continue;
}
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.getExternalFilesDir(null);
}
// Worst case, resort to internal storage
return context.getFilesDir();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static File getBestAvailableFilesRoot(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.getExternalFilesDirs(null);
if (roots != null) {
for (File root : roots) {
if (root == null) {
continue;
}
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.getExternalFilesDir(null);
}
// Worst case, resort to internal storage
return context.getFilesDir();
}