9

Error on Scan UI loading: "com.google.mlkit.common.MlKitException: Failed to scan code."

I have a problem with my device. When the Scan UI is loading (on GmsBarcodeScanning) i'm getting exception addOnFailureListener. "com.google.mlkit.common.MlKitException: Failed to scan code." In other devices i cannot get the same bug... but more than one for sure. Some one have any workaround to fix it? Tnk

Platform: Android Project: com.google.mlkit.samples.codescanner

  • I have the same issue on some devices. I've [reported](https://issuetracker.google.com/issues/261579118) it in Google's Issue Tracker. – ElegyD Feb 13 '23 at 14:24
  • This is likely because of the scanner module is not downloaded yet. Can you check whether it's the case by using ModuleInstallClient API? https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner#configure_your_app – zhouyi Apr 26 '23 at 18:35

3 Answers3

1

There is a workaround.

Always send an install request. If the barcode scanner module is already installed it will not be installed again.

Send an urgent module install request

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val moduleInstall = ModuleInstall.getClient(this)
    val moduleInstallRequest = ModuleInstallRequest.newBuilder()
            .addApi(GmsBarcodeScanning.getClient(this))
            .build()
    moduleInstall
        .installModules(moduleInstallRequest)
        .addOnSuccessListener {
            if (it.areModulesAlreadyInstalled()) {
                // Modules are already installed when the request is sent.
            }
        }
        .addOnFailureListener {
            // Handle failure…
        }
}
Oliver Kranz
  • 3,741
  • 2
  • 26
  • 31
0

Clear the storage data in the google play services app (Settings > Apps > Google Play Service > Storage) and try to update the google play services app. It solved my problem.

Jayakody
  • 1
  • 1
-2

Change metadata value from "barcode_ui" to "barcode" in the AndroidManifest.xml

https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner?hl=en

says that

<meta-data
      android:name="com.google.mlkit.vision.DEPENDENCIES"
      android:value="barcode_ui"/>

This caused problem.

As stated in this link https://developers.google.com/ml-kit/vision/barcode-scanning/android?hl=en

You must use

<meta-data
          android:name="com.google.mlkit.vision.DEPENDENCIES"
          android:value="barcode" >
alperk01
  • 183
  • 1
  • 7