【问题标题】:onActivityResult resultCode is giving vise versa resultonActivityResult resultCode 给出了反之亦然的结果
【发布时间】:2019-02-02 19:55:04
【问题描述】:

我正在使用默认的 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。谁能帮我看看为什么会这样?我没看到什么?

【问题讨论】:

    标签: android android-intent kotlin camera


    【解决方案1】:

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

    解决方案:更改您的代码

    来自

    when {
        requestCode == -1 -> {
    
        }
        requestCode == RESULT_CODE_OK -> {
    
        }
        else -> {
    
        }
    }
    

    when {
        resultCode == Activity.RESULT_OK -> {
    
        }
        resultCode == Activity.RESULT_CANCELED -> {
    
        }
        else -> {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 2015-09-13
      • 2011-03-22
      • 2016-11-12
      • 2019-02-27
      • 2012-12-19
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多