5

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.

Krishna Meena
  • 5,693
  • 5
  • 32
  • 44
  • If your app actually crashes there should be a stacktrace in the logs. – Michael Jul 31 '20 at 13:36
  • 1
    Yes I completely agree with but this not giving any stacktrace. very strange. Then I set no filter in Logcat then I got to know the above mentioned error. Also it is not exactly crashing but my app process it shows as Dead and the camera remains open – Krishna Meena Jul 31 '20 at 14:00
  • To add above I am not sure but may be the camera is killing my app's process – Krishna Meena Jul 31 '20 at 14:01
  • Lesson one: Android OS can kill your activity at any moment if it is not the top activity. In your case the Camera app is on top and indeed your activity can get killed. – blackapps Jul 31 '20 at 14:57
  • `private lateinit var mToolbar: Toolbar private lateinit var mImage: ImageView private var mBitmap: Bitmap? = null` Try to make them local. – blackapps Jul 31 '20 at 15:01
  • Done that but still same error – Krishna Meena Jul 31 '20 at 15:11
  • To further add info My app has multi module feature. From dynamic module 1 I call CaptureImageActivity which is in base module – Krishna Meena Jul 31 '20 at 15:12
  • 1
    Same on my Redmi Note 7. Camera called through `registerForActivityResult(ActivityResultContracts.StartActivityForResult())` with `MediaStore.ACTION_IMAGE_CAPTURE_SECURE` Intent – Ihor Baklykov Apr 22 '22 at 13:56

1 Answers1

2

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.

Velord
  • 46
  • 4
  • I have the same problem in Redmi Note 7 MIUI Global 11.0.1 . could you please tell us more about 'reflash clear android without any shell' ? I don't understand this one – Alexa289 Nov 04 '20 at 08:44
  • I just realise that all apps that need to take image from camera have the same issue. I think the problem is in my phone (Redmi Note 7 MIUI Global 11.0.1) , and unfortunately there is no new update for MIUI – Alexa289 Nov 04 '20 at 08:56
  • @Alexa289, I mean install clear Android via root on your phone. You can find any kind of this solution in [link](https://4pda.ru/forum/index.php?showforum=268). Also, after migrate to latest version of 'implementation ("androidx.fragment:fragment:1.3.0-beta01")' this problem occurs rarely. A have something special undefined behaviour ui on MIUI(11 and 12 ) with custom Toast like [link](https://github.com/Spikeysanju/MotionToast). So is not only Android SDK guilty. – Velord Nov 09 '20 at 14:02
  • @Velort thank you velord, I finally using Camera X library from Google instead of using Intent to take picture. it works very well in Redmi to Android L, no error at all. maybe other developers need this: https://codelabs.developers.google.com/codelabs/camerax-getting-started/#0 – Alexa289 Nov 13 '20 at 07:08