onActivityResult not getting called after changing the intent for (Save the full-size photo)

224 Views Asked by At

Im following the official documentation to capture the image and gets its path. https://developer.android.com/training/camera/photobasics

I have write 100% same code as the documentation say, but two weird things are happing when I write the simple intent

private fun dispatchTakePictureIntent() {
    val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    try {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
    } catch (e: ActivityNotFoundException) {
        // display error state to the user
    }
}

The OnAcivityResult is getting called

But when I change the intent into

private fun dispatchTakePictureIntent() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        // Ensure that there's a camera activity to handle the intent
        takePictureIntent.resolveActivity(packageManager)?.also {
            // Create the File where the photo should go
            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                // Error occurred while creating the File
                ...
                null
            }
            // Continue only if the File was successfully created
            photoFile?.also {
                val photoURI: Uri = FileProvider.getUriForFile(
                        this,
                        "com.example.android.fileprovider",
                        it
                )
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
            }
        }
    }
}

It does get called anymore.

Here is my OnAcivityResult code

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if(requestCode == OPEN_CAMERA)
        if(resultCode == RESULT_OK && data != null) {
            // not getting called
        }

    if(requestCode == OPEN_GALLERY)
        if(resultCode == RESULT_OK && data != null) {
            val image: Uri = data.data!!
            openCropImageView(image)
        }

    if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
        val uri = UCrop.getOutput(data!!)
        val imgFile = File(uri?.path)

        if (imgFile.exists()) {
            val myBitmap = BitmapFactory.decodeFile(imgFile.absolutePath)
            binding.previewImage.setImageBitmap(myBitmap)
        }
    }
}

any help, please.

Edit: Apparently its working fine on a pixel 5 running on android 11 emulator and its not working on Redmi note 5 running on android 8

0

There are 0 best solutions below