Skip to content

Taking Picture

Lucas Yuji edited this page Nov 6, 2022 · 2 revisions

Taking picture

it's very simple to take pictures with Camposer, there are some methods to save with CameraState:

All of these options use the method CameraState.takePicture, which has the last parameter: the callback with image capture result, if successful, it contains the saved URI.

Here is an example using the file:

val cameraState = rememberCameraState()
val context = LocalContext.current
CameraPreview(
  cameraState = cameraState,
) {
  Box(
    onClick = { 
      cameraState.takePicture(file) { result -> 
        if (result is ImageCaptureResult.Success) { // result.savedUri might be useful to you
          Toast.makeText(context, "Success!", Toast.LENGTH_LONG).show()
        }
      } 
    }
  ) {
    Text(text = "Take picture")
  }
}

There's also support to suspend functions! Using suspend functions TakePicture it's return savedUri or throw throwable, so be careful when used it.


If you want to see some example using with androidx ViewModel, check out the Sample Project