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.

7
  • If you empty the onCaptureSuccess method and only keep image.close(), do you see the issue after taking multiple pictures? Jun 8, 2020 at 19:44
  • @HusaynHakeem, Yes, with just image.close() also the issue is still there when taking multiple pictures Jun 9, 2020 at 11:23
  • @HusaynHakeem, Any suggestions? Jun 10, 2020 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. Jun 10, 2020 at 17:18
  • 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. Jun 11, 2020 at 17:17

2 Answers 2

0

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

-1

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

1
  • 1
    Yes, onCaptureSuccess(image); method does close the image. But removing that didn't solve the issue. Jun 7, 2020 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.