Android: Camera2 API crash only on Google Pixel 2 & 2 XL devices

Viewed 658

I'm using the Camera2 sample code in one of my apps from here: https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java

It has been working fine until recently the app crashes on image capturing but only on Google Pixel 2 and Pixel 2 XL devices.

Crash report shows the following:

java.lang.NullPointerException: 
  at ....Camera2BasicFragment$ImageSaver.run (Camera2BasicFragment.java:935)
  at android.os.Handler.handleCallback (Handler.java:873)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:193)
  at android.os.HandlerThread.run (HandlerThread.java:65)

This is line 935 (ImageSaver class):

ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();

I've tried a few things but I'm unable to test as I do not have a Pixel 2 device. Any suggestions?

Update (05-Nov-2018):

So the logical explanation is that the image being returned from the ImageReader is somehow null:

@Override
        public void onImageAvailable(ImageReader reader) {
            mBackgroundHandler.post(new ImageSaver(reader.acquireLatestImage(), mFile));
        }

Some other posts on SO suggest that the ImageReader format may not be supported. This is the format:

mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
 ImageFormat.JPEG, /*maxImages*/2);

But I can't imagine why JPEG would be an unsupported format.

Still looking for an answer!

1 Answers

All phone have triple camera crash with camera2 api, this work for all the phone which have 2 camera on back or single camera

So its advised to change the API to multi camera one rather than restricting that to 2 or 3 cameras

Related