【问题标题】:Camera2 - "Must be called from main thread of fragment host" when changing fragmentCamera2 - 更改片段时“必须从片段主机的主线程调用”
【发布时间】:2019-03-06 04:14:10
【问题描述】:

我正在尝试使用以下代码Google Sample - Camera2Basic 拍摄图像后更改片段。

我在上述示例的第 839 行实现了对 MainActivity 的回调。但是,当我尝试从该回调遍历到不同的活动时,我收到以下异常:

java.lang.IllegalStateException: 必须从主线程调用 分片宿主

有人知道吗?

【问题讨论】:

  • 你能贴出你的回调代码吗?

标签: java android android-fragments exception


【解决方案1】:

我有 Kotlin 中的工作代码 您必须将此回调替换为:

  val captureCallback = object : CameraCaptureSession.CaptureCallback() {

            override fun onCaptureCompleted(session: CameraCaptureSession,
                                            request: CaptureRequest,
                                            result: TotalCaptureResult) {
                sendBackResult(mFile)
            }
        }

        mCaptureSession!!.capture(captureBuilder.build(), captureCallback, mBackgroundHandler)
    } catch (e: CameraAccessException) {
        e.printStackTrace()
    }

sendBackResult 方法如下:

private fun sendBackResult(resultFile: File?) {
    val fileUri = Uri.fromFile(resultFile)
    val dataIntent = Intent()
    dataIntent.data = fileUri
    dataIntent.putExtra("isFront", isFrontCamera)
    activity!!.setResult(Activity.RESULT_OK, dataIntent)
    activity!!.finish()
}

【讨论】:

  • 为帮助喝彩,最终将相机更改为使用 Activity,然后使用结果将图像传回。
猜你喜欢
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多