Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registerForActivityResult TakePicture not triggered

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'
like image 841
Mete Avatar asked Mar 03 '23 08:03

Mete


1 Answers

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.

like image 141
Mete Avatar answered Mar 11 '23 15:03

Mete