gpt4 book ai didi

firebase - 如何修复 “Variable '图片必须初始化”

转载 作者:行者123 更新时间:2023-12-02 13:35:58 25 4
gpt4 key购买 nike

我正在使用Firebase设置TextRecognition,并且“image”变量给我一个“必须初始化变量“image””错误。

我已按照本教程进行操作,到目前为止没有任何问题。

            val image: FirebaseVisionImage
try {
image = FirebaseVisionImage.fromFilePath(this, result.uri)
} catch (e: IOException) {
e.printStackTrace()
}


val detector = FirebaseVision.getInstance()
.onDeviceTextRecognizer
//This is what's giving me an error \/
val myResult = detector.processImage(image)
.addOnSuccessListener { firebaseVisionText ->
// Task completed successfully
// ...
}
.addOnFailureListener {
// Task failed with an exception
// ...
}

我希望我的应用在运行时不会崩溃。

最佳答案

您有两个代码路径,一个在其中初始化图像的路径,另一个在不初始化图像的路径中:

        try {
image = FirebaseVisionImage.fromFilePath(this, result.uri)
} catch (e: IOException) {
e.printStackTrace()
}

如果您的代码经过 FirebaseVisionImage.fromFilePath引发异常的第二条代码路径,则您的代码将继续运行而不会初始化图像。在这种情况下,之后的代码将尝试使用未初始化的 image时失败。

您所要做的就是将与 image一起使用的代码移到 try块中,知道 image具有值的位置,而不是在不确定 image有值的位置移动。

关于firebase - 如何修复 “Variable '图片必须初始化”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com