-1

here is my code

  @Override
public void onActivityResult(int requestCodeC, int resultCode, Intent data) {
    // if this is the result of our camera image request
  

    if (requestCodeC == CAMERA_REQUEST_CODE) {
        // getting bitmap of the image
        Bitmap photo = (Bitmap) Objects.requireNonNull( Objects.requireNonNull( data ).getExtras() ).get( "data" );
        // displaying this bitmap in imageview
        imageView.setImageBitmap( photo );

  }

    super.onActivityResult( requestCodeC, resultCode, data );
}

on back pressed while in camera activity the app crashes, I think this is because data is remaining null. How can I fix this? Thanks,

  • 1
    We need more information to help you. You should provide a better description, the exception stacktrace, screenshots, logs.. anything that can help us to help you. – Messias Lima May 19 '21 at 12:52
  • @Override public void onActivityResult(int requestCodeC, int resultCode, Intent data) { super.onActivityResult( requestCodeC, resultCode, data ); // if this is the result of our camera image request if (requestCodeC == CAMERA_REQUEST_CODE) { // getting bitmap of the image Bitmap photo = (Bitmap) Objects.requireNonNull( Objects.requireNonNull( data ).getExtras() ).get( "data" ); // displaying this bitmap in imageview imageView.setImageBitmap( photo ); } } – ahmad bajwa May 19 '21 at 13:01
  • add this code and run. – ahmad bajwa May 19 '21 at 13:01

1 Answers1

0

First, start by checking resultCode. You are ignoring it. You should see whether or not it is RESULT_OK; if it is not, then do not attempt to read the extra.

Second, be more graceful about handling strange results. There are ~3 billion Android users, using tens of thousands of device models and hundreds of camera apps. Some camera apps will have bugs. Crashing your app, because the camera app has a bug, is not a great user experience.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491