在应用程序中使用相机或图库时,按下backButton会使其崩溃

时间:2018-06-27 11:27:23

标签: android kotlin

我正在尝试在应用程序中使用相机或图库(作为警报)来上传文档。如果按下后退按钮且未选择任何图像,则应用程序将崩溃。

Node node

关于相机的许可没有问题。我没有发布所有代码。

onActivityResult:

  private fun getImageFromCamera() {
    val takePicture = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if (takePicture.resolveActivity(packageManager) != null) {
        startActivityForResult(takePicture, TAKE_PICTURE)
    }
  }

private fun getImageFromGallery() {
    val intent = Intent()
    intent.type = "image/*"
    intent.action = Intent.ACTION_GET_CONTENT
    if (intent.resolveActivity(packageManager) != null) {
        startActivityForResult(Intent.createChooser(intent, "Select 
                                                  Picture"), PICK_IMAGE)
    }
}

这是我得到的例外:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == PICK_IMAGE) {
        if (resultCode == Activity.RESULT_OK) {
            try {
                val inputStream1 = contentResolver.openInputStream(data.data!!)
                bitmap = BitmapFactory.decodeStream(inputStream1)
                mUploadPanCardImageButton!!.setImageBitmap(bitmap)
                mUploadPanCardTextView?.visibility = View.GONE
            } catch (e: FileNotFoundException) {
                e.printStackTrace()
            }
        }
    }else if (requestCode == TAKE_PICTURE) {
        if (resultCode == Activity.RESULT_OK) {
            val extras1 = data.extras
            bitmap = extras1!!.get("data") as Bitmap
            mUploadPanCardImageButton!!.setImageBitmap(bitmap)
            mUploadPanCardTextView?.visibility = View.GONE
        }
    }
}

1 个答案:

答案 0 :(得分:0)

通过放置您的onActivityResult为空值?为了这样的目的-

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {