Skip to content

Instantly share code, notes, and snippets.

@navczydev
Created June 29, 2022 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navczydev/0ceb885ac2cdab0b3e14eb6ff52b2fb7 to your computer and use it in GitHub Desktop.
Save navczydev/0ceb885ac2cdab0b3e14eb6ff52b2fb7 to your computer and use it in GitHub Desktop.
private fun launchCameraIntent() {
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// Ensure that there's a camera activity to handle the intent
// Android11 or higher needs the <query> tag to be added in the Android Manifest file.
// It depend on the device, if by-default camera package is visible or not
takePhotoIntent.resolveActivity(packageManager)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Something went wrong
null
}
// launch camera intent only if the File was successfully created
photoFile?.let {
val photoURI: Uri = // get photo file URI
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePhotoIntent, REQUEST_IMAGE_CAPTURE)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment