-1

I'm attempting to use the basic taking photo intent to return a thumbnail and set it to an image view, as shown on the developer website:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

Expect I'm getting a null object reference

Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity ... java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference ... at line "mImageView.setImageBitmap(imageBitmap);"

UPDATE: Not merely a how to fix null object question, but more importantly how onActivityResult() fits into the rest of the lifecycle. Why does Android forget the assignment of mImageView = findViewById(R.id.image_view); in onCreate() and in onResume when onActivityResult() is called after a camera intent? Does onActivityResult() act as a separate special event within the activity lifecycle? If it does, the developer guide at https://developer.android.com/training/camera/photobasics.html should be updated to include that.

Mr.Drew
  • 939
  • 2
  • 9
  • 30

2 Answers2

1

the variable mImageView is null, you can set reference with findViewById(R.id.idImageView);

Pablo DbSys
  • 532
  • 1
  • 7
  • 17
  • Ah yes, I referenced it in `onCreate()`, but adding it to `onActivityResult()` seemed to do the trick. I read that error as the bitmap was null, not the view. – Mr.Drew Apr 19 '18 at 05:48
1

Cool, your crash is really not a duplicate of the dozen questions that have NPE in onActivityResult of the .

When your onActivityResult() callback is invoked, the mImageView field of your Activity is null. Most likely, this happens because your Activity was created anew by the system after camera fulfilled your Intent. You should put the line

mImageView = findViewById(R.id.your_imageView);

either in onCreate() of your Activity, or you can even do that inside onActivityResult(). onResume() is too late. In any case, you must make sure that the appropriate layout is loaded.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • It only appears to work when `mImageView = findViewById(R.id.your_imageView);` is called within `onActivityResult()`. Even when set in `onCreate` and `onResume` the object is null. – Mr.Drew Apr 20 '18 at 05:36
  • 1
    You are right: `onResume()` is not good in this case, `onActivityResult()` happens before. – Alex Cohn Apr 20 '18 at 10:09