FirebaseVisionImage:变量 'image' 可能尚未初始化

FirebaseVisionImage: variable 'image' might not have been initialized

我正在尝试在 Android Studio 中使用 Google Firebase(机器学习工具包)的示例。将图像变量传递到 detector.processImage 方法时出现错误(错误如下所示)。

我怎样才能克服这个错误?我必须在 try-catch 块中使用 FirebaseVisionImage.fromFilePath,但错误消息告诉我图像变量可能未初始化。

error: variable image might not have been initialized

        FirebaseVisionImage image;

        try {
            image = FirebaseVisionImage.fromFilePath(MainMenuActivity.this,
                    Uri.fromFile(new File("/sdcard/Download/test.jpg")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance().getOnDeviceTextRecognizer();

        Task<FirebaseVisionText> result = detector.processImage(image)
                .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                    @Override
                    public void onSuccess(FirebaseVisionText firebaseVisionText) {
                        // Task completed successfully
                        mainText.setText("Hello!");
                    }
                })
                .addOnFailureListener(
                        new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                // Task failed with an exception
                            }
                        }
                );

由于这段代码出现该错误

FirebaseVisionImage image;

try {
  image = FirebaseVisionImage.fromFilePath(
             MainMenuActivity.this,
             Uri.fromFile(new File("/sdcard/Download/test.jpg"))
          );
} catch (IOException e) {
  e.printStackTrace();
}

现在,问问自己,如果 Exception 被扔进 try 块中,image 会发生什么?
变量 可能 已被赋予有效值,或者它 可能没有 .

因为您让执行流程继续进行(您没有将 Exception 抛出到上层),编译器无法确定这一点,因为异常是在 [=41 处抛出的=]-时间.
因为在 Java 中,局部变量必须在使用前初始化(即使在 = null 中),编译器告诉你这样做。

error: variable image might not have been initialized

解决方案可能是用 null

初始化它
FirebaseVisionImage image = null;

或者,一个更好的,让逃脱Exception。这将表明存在错误。
您还可以从 try - catch 块中完全解包您的代码。

所有需要图像的代码都应该在 try 块内:

try {
    FirebaseVisionImage image = FirebaseVisionImage.fromFilePath(MainMenuActivity.this,
            Uri.fromFile(new File("/sdcard/Download/test.jpg")));
    Task<FirebaseVisionText> result = detector.processImage(image)
        .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
            @Override
            public void onSuccess(FirebaseVisionText firebaseVisionText) {
                // Task completed successfully
                mainText.setText("Hello!");
            }
        })
        .addOnFailureListener(
                new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        // Task failed with an exception
                    }
                }
        );
} catch (IOException e) {
    e.printStackTrace();
}

FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance().getOnDeviceTextRecognizer();

请注意,像您一样只打印运行时异常的 catch 语句通常应该在调用堆栈中尽可能高。所以我建议将它移到所有这些代码的代码中,甚至更高的地方。请记住:如果您根本没有捕获到该错误,Android 也会将其打印出来。因此,请考虑您要通过此 catch 实现的目标并相应地实施它。