I've the below code working fine, that upon button click, the Camera intent is opened, allowing me to take a photo, then confirm the photo is ok, i.e. 2 interfaces from the user after clicking the button, till the pic is loaded in the image view. Can I automate it, i.e. once the user click the button on the activity, camera intent is opened directly, take a photo of whatever there, and return what had been captured to the user.
mm, i.e. something like the apps working with take a selfie
voice command.
My typical code for opening the camera intent is:
btnCamera.setOnClickListener { // I need this click to be the only thing done by the user
if(isPermissionGranted(permission.CAMERA)) startCamera()
else requestCameraPermission(this)
}
private fun startCamera() {
val fileName = System.currentTimeMillis().toString() + ".jpeg"
output = File(
this.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
fileName
)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
outPutFileUri = this.let { it ->
FileProvider.getUriForFile(
it,
BuildConfig.APPLICATION_ID,
output!!
)
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutFileUri)
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) = runBlocking {
super.onActivityResult(requestCode, resultCode, data)
val activity = this
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
val bitmap = outPutFileUri?.let { getCapturedImage(it) }
imageView.setImageBitmap(bitmap)
}
}
private fun getCapturedImage(selectedPhotoUri: Uri): Bitmap =
when {
Build.VERSION.SDK_INT < 28 -> MediaStore.Images.Media.getBitmap(
contentResolver, selectedPhotoUri)
else -> {
val source = ImageDecoder.createSource(contentResolver, selectedPhotoUri)
ImageDecoder.decodeBitmap(source)
}
}
The Camera will be opened, picture will be taken, user will see what is going on the screen,, mm if there is a way to take a pic without opening the camera app it will be welcomed as well. thanks