Image intent and on back press

I have made an image intent where I capture an image and save it in image view but I discovered that if I open the camera and press the back button on phone my app close with an error.

My code for intent:

btnImg.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, 0);
    }
});

My code for bitmap and imageView:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    imgBitmap = (Bitmap) data.getExtras().get("data");
    img.setImageBitmap(imgBitmap);
}

The error:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=0, data=null} to activity {gomugomu.civicalert/gomugomu.civicalert.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:5004)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047)
                  at android.app.ActivityThread.access$1600(ActivityThread.java:229)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7331)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                  at gomugomu.civicalert.MainActivity.onActivityResult(MainActivity.java:83)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7165)

on line 83 is this : imgBitmap = (Bitmap) data.getExtras().get("data");.

I want that when I press the back button to close the camera and go to the previous activity

Upvotes: 1

Views: 1345

Answers (2)

Mehul Patel
Mehul Patel

Reputation: 111

When the user has successfully performed the action, the resultCode will be equal to RESULT_OK. If the user presses the back button then the resultCode will be RESULT_CANCELED. So your code must be modified like this.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
            imgBitmap = (Bitmap) data.getExtras().get("data");
            img.setImageBitmap(imgBitmap);
        }
    }

Upvotes: 1

Ramesh sambu
Ramesh sambu

Reputation: 3539

Try this checking resultCode==RESULT_OK and data!=null with request code == 0

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode == 0 && resultCode == RESULT_OK && data != null) {  
           imgBitmap = (Bitmap) data.getExtras().get("data");
           img.setImageBitmap(imgBitmap);
        }
    }
}

Upvotes: 1

Related Questions