I am developing an app that needs to allow the user to select a Profile picture, and I want to give them an easy option to either take a picture, or choose an existing one from the Gallery.
My searches took me to this Stackoverflow discussion, so I modified the code in one of the answers for my purposes. Here is what I have:
public static Uri openImageIntent(Activity context) {
Uri outputFileUri = null;
File cache_dir = context.getExternalFilesDir("photos");
cache_dir.mkdirs();
File image_file = null;
try {
image_file = File.createTempFile("profile", ".jpg", cache_dir);
} catch (IOException e) {
e.printStackTrace();
}
if (image_file != null) {
File sdImageMainDirectory = new File(cache_dir.getAbsolutePath(), image_file.getName());
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
List<Intent> cameraIntents = new ArrayList<Intent>();
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
context.startActivityForResult(chooserIntent, Constants.RequestCodes.CHOOSE_PICTURE);
}
return outputFileUri;
}
As you can see, it creates a chooser by combining the ACTION_IMAGE_CAPTURE and ACTION_GET_CONTENT intents, with the latter being filtered for "image/*". The goal here is to open up a custom choose that lets the user either pick the Camera app to take a new picture, or choose the Gallery app (or Photos, or a file browser) to pick an existing image. I also had to do some work in onActivityResult() to correctly receive the picture and use it depending on the choice, but I don't think that's relevant here.
On 4.3 and below, this works great. It opens a chooser that looks something like this:
However, on KitKat, the chooser looks like this:
As you can see, it seems to be ignoring the "image/*" filter, and just giving me some generic "Documents" app for opening files.
Obviously something has changed in KitKat, but I don't know what it is, and I can't find anyone else who has encountered this problem.
Any ideas?