Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 5.1.1 default camera return empty intent in onActivityResult after capturing image

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!

like image 812
passer Avatar asked Jun 18 '15 04:06

passer


1 Answers

I have added one more condition. It seems working fine without any problem in 5.1.1 as well as in different API levels

if(data == null){
 isCamera = true;
}else if(data.getData() == null){
 isCamera = true;
}
else{
  //....
like image 53
Ponsuyambu Avatar answered Sep 19 '22 13:09

Ponsuyambu