0Pricing
Android Academy · Lesson

Showing a Camera Preview

Wire up a live preview.

Goal: A Live Preview

The first thing almost every camera app needs is a live preview — a real-time view of what the camera sees. In this lesson you'll build a Compose screen that shows the camera feed full-screen.

You'll combine three pieces: a PreviewView, the Preview use case and a bound ProcessCameraProvider.

Requesting the Camera Permission

Before you can show a preview you need the user's permission. In Compose, Accompanist (or the official permissions API) makes this easy with rememberPermissionState.

Only start the camera once the permission is granted.

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun CameraScreen() {
    val permission = rememberPermissionState(Manifest.permission.CAMERA)

    LaunchedEffect(Unit) { permission.launchPermissionRequest() }

    if (permission.status.isGranted) {
        CameraPreview()
    } else {
        Text("Camera permission is required.")
    }
}

All lessons in this course

  1. CameraX Overview
  2. Showing a Camera Preview
  3. Taking Photos
  4. Recording Video
← Back to Android Academy