【问题标题】:App crashes when launching camera to take picture on Redmi 7A在Redmi 7A上启动相机拍照时应用程序崩溃
【发布时间】:2020-11-21 06:08:56
【问题描述】:

当我在 Redmi 7A 上启动相机拍照时,我的应用程序自动崩溃。以下是崩溃日志:

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

请帮助它确实没有显示问题到底是什么。

我的活动代码

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)
        }
    }
}

此外,应用程序并非每次都崩溃,而只是偶尔崩溃。

【问题讨论】:

  • 如果您的应用确实崩溃了,日志中应该有一个堆栈跟踪。
  • 是的,我完全同意,但这并没有给出任何堆栈跟踪。很奇怪。然后我在 Logcat 中没有设置过滤器,然后我就知道了上面提到的错误。此外,它并没有完全崩溃,但我的应用程序进程显示为 Dead 并且相机保持打开状态
  • 添加上面我不确定,但可能是相机正在杀死我的应用程序的进程
  • 第一课:Android OS 可以随时杀死您的活动,如果它不是顶级活动。在您的情况下,相机应用程序位于顶部,您的活动确实可能会被杀死。
  • private lateinit var mToolbar: Toolbar private lateinit var mImage: ImageView private var mBitmap: Bitmap? = null 尝试将它们本地化。

标签: android kotlin android-camera android-camera-intent redmi-device


【解决方案1】:

我的 Redmi Note 8t (MIUI Global 11.04.1) 也有同样的问题。但在 Redmi Note 8 Pro (MIUI Global 12.0.2) 上一切正常。看来这是小米MIUI的问题。一种解决方法是尝试安装其他 MIUI 或在没有任何外壳的情况下重新刷新清除 android。

【讨论】:

  • 我在 Redmi Note 7 MIUI Global 11.0.1 中遇到了同样的问题。你能告诉我们更多关于“没有任何外壳的刷新清除android”的信息吗?这个看不懂
  • 我只是意识到所有需要从相机拍摄图像的应用程序都有同样的问题。我认为问题出在我的手机上(Redmi Note 7 MIUI Global 11.0.1),不幸的是MIUI没有新的更新
  • @Alexa289,我的意思是在你的手机上通过 root 安装 clear Android。您可以在link 中找到任何类型的解决方案。此外,在迁移到最新版本的 'implementation ("androidx.fragment:fragment:1.3.0-beta01")' 后,这个问题很少发生。 A 在 MIUI(11 和 12) 上有一些特殊的未定义行为 ui,带有像 link 这样的自定义 Toast。因此,不仅 Android SDK 有罪。
  • @Velort 谢谢velord,我终于用谷歌的Camera X库代替了Intent拍照。它在 Redmi 到 Android L 中运行良好,完全没有错误。也许其他开发人员需要这个:codelabs.developers.google.com/codelabs/camerax-getting-started/…
猜你喜欢
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多