0

Whenever a second picture is taken getting this particular error:

    D/CaptureSession: Issuing capture request.
W/ImageReader_JNI: Unable to acquire a buffer item, very likely client tried to acquire more than maxImages buffers
D/MetadataImageReader: Failed to acquire next image.
    java.lang.IllegalStateException: maxImages (2) has already been acquired, call #close before acquiring more.
        at android.media.ImageReader.acquireNextImage(ImageReader.java:513)
        at androidx.camera.core.AndroidImageReaderProxy.acquireNextImage(AndroidImageReaderProxy.java:79)
        at androidx.camera.core.MetadataImageReader.imageIncoming(MetadataImageReader.java:318)
        at androidx.camera.core.MetadataImageReader$2.onImageAvailable(MetadataImageReader.java:67)
        at androidx.camera.core.AndroidImageReaderProxy$1$1.run(AndroidImageReaderProxy.java:145)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

As per my understand we have to close the image before clicking the next one but I have even done that, still getting the same error

Here is my code:

    private void takePicture()  {
    imageCapture.setFlashMode(flashMode);
    imageCapture.takePicture(cameraExecutor, new ImageCapture.OnImageCapturedCallback() {
        @SuppressLint("UnsafeExperimentalUsageError")
        @Override
        public void onCaptureSuccess(@NonNull ImageProxy image) {
            Bitmap imageBitmap = rotateImage(imageToBitmap(Objects.requireNonNull(image.getImage())), image.getImageInfo().getRotationDegrees());
            try {
                FileUtil.saveBitmap(MainActivity.this, imageBitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }
            image.close();
            super.onCaptureSuccess(image);
        }
        @Override
        public void onError(@NonNull ImageCaptureException exception) {
            super.onError(exception);
        }
    });
}

As seen in the code, I'm closing the image. Still, it did not solve the issue.

Answers such as: Android camera2: java.lang.IllegalStateException: maxImages (1) has already been acquired, call #close before acquiring more

Suggested to close the image in ImageReader.OnImageAvailableListener, but through CameraX we are not directly dealing with ImageReader.

Please suggest a method to resolve this issue.

Prajwal Bhat
  • 75
  • 1
  • 8
  • If you empty the `onCaptureSuccess` method and only keep `image.close()`, do you see the issue after taking multiple pictures? – Husayn Hakeem Jun 08 '20 at 19:44
  • @HusaynHakeem, Yes, with just image.close() also the issue is still there when taking multiple pictures – Prajwal Bhat Jun 09 '20 at 11:23
  • @HusaynHakeem, Any suggestions? – Prajwal Bhat Jun 10 '20 at 15:11
  • This seems like a weird bug. a) Do you have your code somewhere I can pull it and test it? (on github for instance). b) Could you update to beta05 (which was released today), and verify if the issue still occurs. – Husayn Hakeem Jun 10 '20 at 17:18
  • a) You can find the code in this repo: https://github.com/prajwal-bhat/CameraX b) Updated to beta05 and still issue is there. – Prajwal Bhat Jun 10 '20 at 18:02
  • 1
    Testing your code, it seems the app doesn't crash due to that exception. By clicking on the image capture button multiple times, all images are taken successfully. Is that the behavior you're also seeing? As for the exception stacktrace being displayed in the logcat, it's thrown internally by CameraX. The image capture use case handles a pipeline on incoming capture requests, and only takes 1 picture at a time. I'm guessing this stacktrace can be ignored if your app isn't crashing and the pictures are being stored successfully. You can also create an issue and file it to CameraX. – Husayn Hakeem Jun 11 '20 at 17:17
  • @HusaynHakeem, Thanks for your time and help. I agree with the app is not crashing. I have tested with 3 different phones and it didn't crash in any of the phones. But not all the images are taken successfully. For example if the capture button is clicked 5 times and if 2 times the exception is captured in logcat then only 3 images are being stored in memory. I have tested this case with other phones as well. is that the case with you? and Sure, I will create an issue in CameraX. – Prajwal Bhat Jun 11 '20 at 17:38

2 Answers2

0

The super.onCaptureSuccess(image) should be at the beginning of the onCaptureSuccess function. You call super.onCaptureSuccess(image) after closing the image.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Claudio
  • 86
  • 6
-1

analyzing super.onCaptureSuccess(image); he calls image.close(), so you call close twice, try remove super from onCaptureSuccess method