I trying to new registerForActivityResult for taking picture. I can open Camera Intent, but after taking picture, callback is not triggered and i can't see anything about Activity Result or an error on logcat.
I tried also RequestPermission, it's triggered. I couldn't find, what's wrong.
My code is here:
class UploadDocumentFragment {
private val registerTakePicture = registerForActivityResult(
ActivityResultContracts.TakePicture()
) { isSuccess ->
if (isSuccess) {
viewModel.addDocToRequest()
viewModel.setSelectedDocument(null)
} else {
R.string.internal_error.showAsDialog { }
}
}
//...
private fun takeImage() {
val photoFile: File? = viewModel.createImageFile()
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
requireContext(),
BuildConfig.APPLICATION_ID +".fileProvider",
it
)
registerTakePicture.launch(photoURI)
}
}
}
createImageFile function on ViewModel:
@Throws(IOException::class)
fun createImageFile(): File? {
val imageFileName = selectedDocumentTypeLD.value?.visibleName
return try {
val file = File(storageDir, "$imageFileName.jpg")
if (file.createNewFile() || file.exists()) {
file
} else {
null
}
} catch (ex: IOException) {
ex.printStackTrace()
null
}
}
App gradle:
implementation 'androidx.activity:activity-ktx:1.2.0-alpha06'
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha06'
I found the problem.
My previous onActivityResult function was still there. I thought maybe the old function could override registerForActivityResult. When I remove the old function, registerForActivityResult works very well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With