I have a problem with emulated camera on AVD. I use camera for QR/ barcode scanning, and normally it works just fine, but once in a while after the successful scan, when I reopen the screen with Camera Preview again it shows only black screen, it also affects the built-in camera app. However, I've never encountered this issue during the tests on physical devices. It shouldn't be a problem with permission, since the camera stops working after a few successful camera usages. It happens on API 24, API 33 and also API 34.
Camera implementation:
AndroidView(
modifier = modifier.fillMaxSize(),
factory = { context ->
val previewView = PreviewView(context)
val preview = Preview.Builder().build()
val selector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
preview.setSurfaceProvider(previewView.surfaceProvider)
val imageAnalysis = ImageAnalysis.Builder().build()
imageAnalysis.setAnalyzer(
ContextCompat.getMainExecutor(context),
BarcodeAnalyzer(
context,
onSuccess = {
onFinished(it)
cameraProviderFuture.get().unbindAll()
}
)
)
runCatching {
cameraProviderFuture.get().bindToLifecycle(
lifecycleOwner,
selector,
preview,
imageAnalysis
)
}.onFailure {
Log.e("CAMERA", "Camera bind error ${it.localizedMessage}", it)
}
previewView
})
Source: View source