Solution 1 :
The super.onCaptureSuccess(image)
should be at the beginning of the onCaptureSuccess
function. You call super.onCaptureSuccess(image)
after closing the image.
Solution 2 :
analyzing super.onCaptureSuccess(image);
he calls image.close()
, so you call close twice, try remove super from onCaptureSuccess method
Problem :
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.
Comments
Comment posted by Husayn Hakeem
If you empty the
Comment posted by Prajwal Bhat
@HusaynHakeem, Yes, with just image.close() also the issue is still there when taking multiple pictures
Comment posted by Prajwal Bhat
@HusaynHakeem, Any suggestions?
Comment posted by Husayn Hakeem
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.
Comment posted by Husayn Hakeem
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.
Comment posted by Prajwal Bhat
Yes, onCaptureSuccess(image); method does close the image. But removing that didn’t solve the issue.