Search code examples
androidandroid-camera-intent

android camera: onActivityResult() intent is null if it had extras


After searching a lot in all the related issues at Stack Overflow and finding nothing, please try to help me.

I created an intent for capture a picture. Then I saw different behavior at onActivityResult(): if I don't put any extra in the Intent (for small pics) the Intent in onActivityResult is ok, but when I put extras in the intent for writing the pic to a file, the intent in onActivityResult is null!

The Intent creation:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// without the following line the intent is ok
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);

Why is it null, and how can I solve it?


Solution

  • It happens the same to me, if you are providing MediaStore.EXTRA_OUTPUT, then the intent is null, but you will have the photo in the file you provided (Uri.fromFile(f)).

    If you don't specify MediaStore.EXTRA_OUTPUT then you will have an intent which contains the uri from the file where the camera has saved the photo.

    Don't know if it as a bug, but it works that way.

    EDIT: So in onActivityResult() you no longer need to check for data if null. The following worked with me:

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PICK_IMAGE_REQUEST://actionCode
                if (resultCode == RESULT_OK && data != null && data.getData() != null) {
                    //For Image Gallery
                }
                return;
    
            case CAPTURE_IMAGE_REQUEST://actionCode
                if (resultCode == RESULT_OK) {
                    //For CAMERA
                    //You can use image PATH that you already created its file by the intent that launched the CAMERA (MediaStore.EXTRA_OUTPUT)
                    return;
                }
        }
    }
    

    Hope it helps