camera-samples
camera-samples copied to clipboard
Found bug in logic when making photo very often (tapping take photo button fast and continuously)
So there is a bug when coroutine can endlessly wait https://github.com/android/camera-samples/blob/main/Camera2Basic/app/src/main/java/com/example/android/camera2/basic/fragments/CameraFragment.kt#L372
It may happen if you make many photos by tapping take photo button fast and continuously for some time
The solution is to safely quit this while
statement before resuming the coroutine with the result:
var image: Image? = null
while (image == null) { // ADD CONDITION TO SAFELY QUIT WHILE!!!
// Dequeue images while timestamps don't match
image = imageQueue.take()
// TODO(owahltinez): b/142011420
// if (image.timestamp != resultTimestamp) continue
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
&& image.timestamp != resultTimestamp
) {
image = null // RESET TO NULL IF NOT CORRECT ONE
continue
}
if (DEBUG) Timber.d("Matching image dequeued: ${image.timestamp}")
// Unset the image reader listener
Timber.d("takePhotoooo Runnable removeCallbacks")
imageReaderHandler.removeCallbacks(timeoutRunnable)
imageReader.setOnImageAvailableListener(null, null)
// Clear the queue of images, if there are left
while (imageQueue.size > 0) {
imageQueue.take().close()
}
val rotation = relativeOrientation.value ?: 0
val mirrored = characteristics.get(CameraCharacteristics.LENS_FACING) ==
CameraCharacteristics.LENS_FACING_FRONT
val exifOrientation = computeExifOrientation(rotation, mirrored)
// Build the result and resume progress
//cont.resume(CombinedCaptureResult(image, result, exifOrientation, imageReader.imageFormat))
// NOT SAFE!!!!!!
// There is no need to break out of the loop, this coroutine will suspend // IT'S NOT RIGHT STILL, BETTER BREAK IT AND CONTINUE COROUTINE AFTER IT!!!
}
cont.resume(CombinedCaptureResult(image, result, exifOrientation, imageReader.imageFormat))
// THIS IS MUCH BETTER