1

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

Tehleel Mir
  • 743
  • 8
  • 27
  • "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" -- that camera app may be crashing when trying to use `EXTRA_OUTPUT`. Using `ACTION_IMAGE_CAPTURE` is fine, but it should only be for cases where you do not mind these sorts of problems. – CommonsWare Apr 17 '22 at 11:07
  • Yes you were right, the camera app is crashing that's why I was not getting any log errors. any way to solve this problem? – Tehleel Mir Apr 18 '22 at 04:00
  • 1
    Since Redmi is unlikely to fix their phone app, no, sorry, there is no way to solve the problem directly. You could switch to using a camera library (Google's CameraX, Fotoapparat, CameraKit/Android, etc.) and take the photo directly within your own app, if you wish. – CommonsWare Apr 18 '22 at 11:20

0 Answers0