'Provided camera selector unable to resolve a camera for the given use case
I use camera in compose to take picture. Code for camera preview^
@Composable
fun CameraPreview(
modifier: Modifier = Modifier,
scaleType: PreviewView.ScaleType = PreviewView.ScaleType.FILL_CENTER,
onUseCase: (UseCase) -> Unit = { }
) {
AndroidView(
modifier = modifier,
factory = { context ->
val previewView = PreviewView(context).apply {
this.scaleType = scaleType
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
onUseCase(
Preview.Builder()
.build()
.also {
it.setSurfaceProvider(previewView.surfaceProvider)
}
)
previewView
}
)
}
Code for take picture
@Composable
fun CameraCapture(
modifier: Modifier = Modifier,
cameraSelector: CameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA,
onPhotoCreated: (Bitmap, String) -> Unit = { bitmap, string -> bitmap; string},
onClose: () -> Unit,
onFullScreen: (Boolean) -> Unit = {},
systemUiController: SystemUiController = rememberSystemUiController()
) {
var isFullScreen by remember { mutableStateOf(true) }
onFullScreen(isFullScreen)
WindowCutoutMode(isFullScreen = isFullScreen)
SystemBarColor(
systemUiController = systemUiController,
specialStatusColor = Color.Transparent
)
Box(modifier = modifier) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
var previewUseCase by remember { mutableStateOf<UseCase>(Preview.Builder().build()) }
CameraPreview(
modifier = Modifier.fillMaxSize(),
onUseCase = {
previewUseCase = it
}
)
var newSelector by remember { mutableStateOf(cameraSelector) }
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
val cameraProvider = cameraProviderFuture.get()
val cameraExecutor: ExecutorService = Executors.newSingleThreadExecutor()
val imageCaptureUseCase by remember {
mutableStateOf(
ImageCapture.Builder()
.setCaptureMode(CAPTURE_MODE_MAXIMIZE_QUALITY)
.build()
)
}
val coroutineScope = rememberCoroutineScope()
Box(
modifier = Modifier
.align(Alignment.TopStart)
.padding(top = 48.dp, start = 16.dp)
.clip(CircleShape)
.background(Colors.Background.transparent)
) {
Icon(
imageVector = Icons.Rounded.Close,
contentDescription = null,
modifier = Modifier
.clickable(
role = Role.Button
) {
isFullScreen = false
onClose()
},
)
}
Row(
modifier = Modifier
.fillMaxWidth()
.background(Colors.Background.light)
.align(Alignment.BottomCenter),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
GalleryImagePicker(onPhotoCreated)
Box(
modifier = Modifier
.size(100.dp)
.padding(16.dp)
.clip(CircleShape)
.background(Color.White),
) {
Box(
modifier = Modifier
.size(80.dp)
.padding(5.dp)
.clickable(
role = Role.Button
) {
coroutineScope.launch(Dispatchers.IO) {
val imageFile = imageCaptureUseCase.takePicture(cameraExecutor)
onPhotoCreated(rotateByOrientation(imageFile), imageFile.name)
}
}
.clip(CircleShape)
.background(Color.Black),
) {
}
}
Box(
modifier = Modifier
.size(80.dp)
.padding(16.dp)
.clip(CircleShape)
.background(Color.White),
) {
Box(
modifier = Modifier
.size(60.dp)
.padding(5.dp)
.clip(CircleShape)
.background(Color.Black),
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_camera_change),
contentDescription = "",
tint = Color.White,
modifier = Modifier
.align(Alignment.Center)
.clickable(
role = Role.Button
) {
newSelector =
if (newSelector == CameraSelector.DEFAULT_FRONT_CAMERA)
CameraSelector.DEFAULT_BACK_CAMERA
else
CameraSelector.DEFAULT_FRONT_CAMERA
}
)
}
}
}
LaunchedEffect(newSelector) {
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
lifecycleOwner,
newSelector,
previewUseCase,
imageCaptureUseCase
)
} catch (ex: Exception) {
Log.e("CameraCapture", "Failed to bind camera use cases", ex)
}
}
}
}
The code works fine all time, but suddenly (on this week) it started to thorws an exceptions like
java.lang.IllegalArgumentException: Provided camera selector unable to resolve a camera for the given use case
with message Failed to bind camera use cases. When view opens it has only black screen. The code didn't changes and i can't see the error.
Versions:
"androidx.camera:camera-core:1.0.1""
"androidx.camera:camera-camera2:1.0.1""
"androidx.camera:camera-lifecycle:1.0.1""
"androidx.camera:camera-view:1.1.0-beta02"
"androidx.camera:camera-extensions:1.1.0-beta02"
UPDATE
Some more exceptions from log:
Camera LensFacing verification failed, existing cameras: []
CameraValidator$CameraIdListIncorrectException: Expected camera missing from device.
UPDATE 2
After device reboot exception gone, but preview is black screen and it spam every second log messages like
D/Camera2CameraImpl: {Camera@739281d[id=1]} Transitioning camera internal state: OPENED --> CLOSING
Solution 1:[1]
The permission has to be re-granted.
EDIT: Have you ensured that no other apps are using the camera at the moment? If the internal state was OPENED, maybe it is clashing with another source utilising the resource.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |