onActivityResult resultCode反之亦然

时间:2018-08-28 08:35:55

标签: android android-intent kotlin camera

我正在使用默认的android Image Capture意向,但遇到了问题。这是我的密码

 fun onCameraClick(): View.OnClickListener {
        return View.OnClickListener {
            val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)

            if (intent.resolveActivity(dialog.context.packageManager) != null) {
                var photoFile: File? = null

                try {
                    photoFile = dialog.context.createImageFile()
                } catch (e: IOException) {
                    Timber.e(e)
                }

                photoFile?.let {
                    val photoURI = FileProvider.getUriForFile(
                            dialog.context,
                            "${dialog.context.packageName}.fileprovider",
                            it
                    )
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                    generalBottomSheetDialogFragment?.startActivityForResult(intent, CAMERA_REQUEST_CODE)
                }
            }
        }
    }

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

        when (requestCode) {
            CAMERA_REQUEST_CODE -> {

                when {
                    requestCode == -1 -> {

                    }
                    requestCode == RESULT_CODE_OK -> {

                    }
                    else -> {

                    }
                }

            }
            PDF_REQUEST_CODE ->
                data?.also {
                    generalResponse.setElement(it)
                    dialog.dismiss()
                }
        }


    }

我的问题是,当我成功拍摄照片时,resultCode将为-1,而当我从相机上按回时,将得到RESULT_CODE_OK。谁能帮我看看为什么会这样吗?我没看到什么?

1 个答案:

答案 0 :(得分:1)

根本原因:因为您的代码不正确

解决方案:更改代码

来自

when {
    requestCode == -1 -> {

    }
    requestCode == RESULT_CODE_OK -> {

    }
    else -> {

    }
}

收件人

when {
    resultCode == Activity.RESULT_OK -> {

    }
    resultCode == Activity.RESULT_CANCELED -> {

    }
    else -> {

    }
}