I have a camera screen whose preview used to work ...
# compose
j
I have a camera screen whose preview used to work but unfortunately hasn't since the update to stable artic fox for some reason. Code in the thread
Copy code
@androidx.compose.ui.tooling.preview.Preview
@Composable
fun CameraPreviewPreview() {
    val imageCapture = ImageCapture.Builder().build()
    CameraPreview(imageCapture = imageCapture)
}

@Composable
fun CameraPreview(imageCapture: ImageCapture) {
    val lifecycleOwner = LocalLifecycleOwner.current
    val context = LocalContext.current
    val cameraProviderFuture = remember { ProcessCameraProvider.getInstance(context) }

    AndroidView(
        factory = { ctx ->
            val previewView = PreviewView(ctx).apply {
                implementationMode = PreviewView.ImplementationMode.COMPATIBLE
            }
            val executor = ContextCompat.getMainExecutor(context)

            cameraProviderFuture.addListener({
                val cameraProvider = cameraProviderFuture.get()
                val preview = Preview.Builder().build().also {
                    it.setSurfaceProvider(previewView.surfaceProvider)
                }

                val cameraSelector = CameraSelector.Builder()
                    .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                    .build()

                cameraProvider.unbindAll()
                cameraProvider.bindToLifecycle(
                    lifecycleOwner,
                    cameraSelector,
                    preview,
                    imageCapture,
                )
            }, executor)
            previewView
        },
    )
}
c
Are you able to see the error in the Preview? There’s an issues panel in there that should have a red !
j
There appears to be no errors since it builds fine. Actually, I just noticed if I looked a little closer, there was a small dot that I guess would be the preview. I had no idea why it's showing so small, so I added a modifier to increase the composable to its max size. It seems to have done the trick.
Copy code
@androidx.compose.ui.tooling.preview.Preview
@Composable
fun CameraPreviewPreview() {
    val imageCapture = ImageCapture.Builder().build()
    CameraPreview(imageCapture = imageCapture, modifier = Modifier.fillMaxSize())
}

@Composable
fun CameraPreview(imageCapture: ImageCapture, modifier: Modifier = Modifier) {
    val lifecycleOwner = LocalLifecycleOwner.current
    val context = LocalContext.current
    val cameraProviderFuture = remember { ProcessCameraProvider.getInstance(context) }

    AndroidView(
        factory = { ctx ->
            val previewView = PreviewView(ctx).apply {
                implementationMode = PreviewView.ImplementationMode.COMPATIBLE
            }
            val executor = ContextCompat.getMainExecutor(context)

            cameraProviderFuture.addListener({
                val cameraProvider = cameraProviderFuture.get()
                val preview = Preview.Builder().build().also {
                    it.setSurfaceProvider(previewView.surfaceProvider)
                }

                val cameraSelector = CameraSelector.Builder()
                    .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                    .build()

                cameraProvider.unbindAll()
                cameraProvider.bindToLifecycle(
                    lifecycleOwner,
                    cameraSelector,
                    preview,
                    imageCapture,
                )
            }, executor)
            previewView
        },
        modifier // <- adding a modifier with .fillMaxSize() fixed the issue
    )
}
c
Hmm interesting - do you have a before/after screenshot of the Preview with your modifier change?
You can also set the width and height of the Preview manually, or set it to a device
j
Before: (Maybe I was imagining the dot 😅)
AfteR:
c
I see - it might be because Preview treated the content size differently before. Seems at runtime, the content does take up all the available space. I don’t know if this is a bug or expected… ccing @nosuid
n
@Justin Yue, I’m having issues currently where a preview for a CameraX view is giving me an exception. Our code is very similar:
java.lang.IllegalStateException: It is not allowed to create new threads in the preview
Are you also seeing this? Maybe it’s related?
j
I have not seen that before. If it helps with your exception, my code is based off of this article: https://link.medium.com/k5E53xtwPib
n
Thanks. That’s where I got my code too.
j
Hmmm this is strange. Even with the preview throwing an exception, does the code still run?
n
sometimes, I think it keeps retrying then eventually works.
j
When you say retrying, is the camera slow to load, or do u have to open the app several times before the camera loads
n
The preview pane keeps on rendering over and over again in Android Studio