App crashes when launching camera to take picture on Redmi 7A

3.2k Views Asked by At

My app automatically crashes when I launch camera to take picture on Redmi 7A. Following are the crash logs:

   E/CAM_CameraIntentManager: checkCallerLegality: Unknown caller: com.qikcircle.eclinic

Please help it really does not show what exactly the issue is.

My Activity Code

class CaptureImageActivity : AppCompatActivity() {

    private lateinit var mToolbar: Toolbar
    private lateinit var mImage: ImageView
    private var mBitmap: Bitmap? = null

    private val getCameraAndStoragePermission = registerForActivityResult(
            ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        if (hasAllPermissionsGranted(permissions)) {
            dispatchTakePictureIntent()
        } else {
            Toast.makeText(
                    this,
                    resources.getString(R.string.allow_camera_permission),
                    Toast.LENGTH_SHORT)
                    .show()
            finish()
        }
    }

    private val takePicture =
            registerForActivityResult(ActivityResultContracts.TakePicture()) { success ->
        if (success) {
            photoURI.let {
                mBitmap = getBitmap(this, photoURI)
                mImage.setImageBitmap(mBitmap)
                goBack()
            }
        } else {
            Toast.makeText(
                    this,
                    resources.getString(R.string.something_went_wrong),
                    Toast.LENGTH_SHORT)
                    .show()
            finish()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_capture_image)

        setupToolbar()

        mImage = findViewById(R.id.image)
        val mSend = findViewById<ImageButton>(R.id.send)
        mSend.setOnClickListener {
            goBack()
        }

        checkCameraPermission()
    }

    private fun setupToolbar() {
        mToolbar = findViewById(R.id.toolbar)
        val title: TextView = findViewById(R.id.title)
        title.text = resources.getString(R.string.app_name)
        setSupportActionBar(mToolbar)
        showBackButton()
    }

    private fun showBackButton() {
        if (supportActionBar != null) {
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)
            supportActionBar!!.setDisplayShowHomeEnabled(true)
        }
        mToolbar.setNavigationOnClickListener {
            super.onBackPressed()
        }
    }

    private fun checkCameraPermission() {
        val permissions = arrayOf(
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        if (!hasPermissions(this, *permissions)) {
            getCameraAndStoragePermission.launch(permissions)
            return
        }
        dispatchTakePictureIntent()
    }

    private fun goBack() {
        val intent = Intent()
        intent.data = photoURI
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    private lateinit var currentPhotoPath: String

    @Throws(IOException::class)
    private fun createImageFile(): File {
        // Create an image file name
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())
        val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        return File.createTempFile(
                "JPEG_${timeStamp}_", /* prefix */
                ".jpg", /* suffix */
                storageDir /* directory */
        ).apply {
            // Save a file: path for use with ACTION_VIEW intents
            currentPhotoPath = absolutePath
        }
    }

    private lateinit var photoURI: Uri

    private fun dispatchTakePictureIntent() {
        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 {
            photoURI = FileProvider.getUriForFile(
                    this,
                    "com.qikcircle.eclinic.fileprovider",
                    it
            )
            takePicture.launch(photoURI)
        }
    }
}

Also app is not crashing every time but some times only.

1

There are 1 best solutions below

4
On

I have same problem on my Redmi Note 8t (MIUI Global 11.04.1). But on Redmi Note 8 Pro (MIUI Global 12.0.2) everything works fine. Looks like this is problem of Xiaomi MIUI. One way solution is try install other MIUI or reflash clear android without any shell.