I have the following code which request user to pick an image from photo apps or capture an image by camera apps:
// Camera
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = fragment.getActivity().getPackageManager();
final 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[]{}));
fragment.startActivityForResult(chooserIntent, UPLOAD_IMAGE_ACTIVITY_REQUEST_CODE);
And my code of onActivityResult:
if(requestCode == UPLOAD_IMAGE_ACTIVITY_REQUEST_CODE)
{
final boolean isCamera;
if(data == null)
{
isCamera = true;
}
else
{
final String action = data.getAction(); // data is always empty here after capture image by default camera in 5.1.1!
if(action == null)
{
isCamera = false;
}
else
{
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
//do sth according to value of isCamera
}
These codes work well in the previous android versions. However, when I updated my nexus 5 to Android 5.1.1 (together updating the camera app to latest version), the codes doesn't work well when requesting default camera to capture photos.
According to debugger, when the code reaches final String action = data.getAction();
after capturing an image by the default camera app, the result Intent data
is always an empty Intent (not null though) that contains no action , extras, data, etc. So final String action = data.getAction();
always return null and fails my following codes.
I guess something is changed for the default camera app in 5.1.1 so the camera intent behavior is different. But then I get no idea on how to make it works.
Any suggestions would be appreciate. Thanks!
Your guess is correct, there is a change in Lollipop: http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
You need to set the ClipData in the intent, that's how i do it
in your case i think it is
Also i don't set MediaStore.EXTRA_OUTPUT, because for me it returns null data, i dont't know how you don't get a null data, setting MediaStore.EXTRA_OUTPUT, but that's another thing: Camera activity returning null android