I'm building a Flutter plugin (mlkit_document_scanner). The purpose of the plugin is to provide a wrapper around the new MLKit Document Scanner. My problem is that I cannot observe an event in which the MLKit Document Scanner is exited without an image taken.
Here's how I'm launching scanner activity:
// Construct document scanner
val scanner = library.buildGmsDocumentScanner(
maximumNumberOfPages = call.argument<Int>(ARGUMENT_NUMBER_OF_PAGES) ?: 1,
galleryImportAllowed = call.argument<Boolean>(ARGUMENT_GALLERY_IMPORT_ALLOWED)
?: false,
scannerMode = call.argument<Int>(ARGUMENT_SCANNER_MODE)?.let {
val allowedScannerModes = listOf(
GmsDocumentScannerOptions.SCANNER_MODE_FULL,
GmsDocumentScannerOptions.SCANNER_MODE_BASE,
GmsDocumentScannerOptions.SCANNER_MODE_BASE_WITH_FILTER
)
if (allowedScannerModes.contains(it)) it else GmsDocumentScannerOptions.SCANNER_MODE_FULL
} ?: GmsDocumentScannerOptions.SCANNER_MODE_FULL,
resultMode = DocumentScannerResultMode.values()[call.argument<Int>(
ARGUMENT_RESULT_MODE
) ?: 2],
)
// Launch document scanner activity, and collect failure if it occurs
activity?.let {
scanner
.getStartScanIntent(it)
.addOnSuccessListener { intentSender ->
Log.d(LOGGING_TAG, "Launching document scanner")
documentScannerLauncher?.launch(
IntentSenderRequest.Builder(intentSender).build()
)
}
.addOnFailureListener { exception ->
Log.e(LOGGING_TAG, "MLKit threw exception:\n" + exception.message)
result.error(
ERROR_CODE_START_SCAN_INTENT_FAILURE,
ERROR_MESSAGE_START_SCAN_INTENT_FAILURE,
exception
)
}
}
I have the following code for collecting results from the launched activity.
fun handleActivityResult(
data: Intent?,
eventSinkJPEG: EventChannel.EventSink?,
eventSinkPDF: EventChannel.EventSink?
) {
val documentScanningResult = GmsDocumentScanningResult.fromActivityResultIntent(data)
if (documentScanningResult == null) {
Log.d(LOGGING_TAG, "NOTHING CAME BACK FROM THE SCAN!!!")
return
}
documentScanningResult.also { result ->
result.pages.let {
if (it.isNullOrEmpty()) {
Log.d(LOGGING_TAG, "null response (result.pages)")
eventSinkJPEG?.success(null)
} else {
Log.d(LOGGING_TAG, "JPEG pages count ${it.size}")
eventSinkJPEG?.success(it.map { page ->
page.imageUri.toFile().readBytes()
})
}
}
result.pdf.let {
if (it != null && it.pageCount > 0) {
Log.d(LOGGING_TAG, "PDF pages count ${it.pageCount}")
eventSinkPDF?.success(it.uri.toFile().readBytes())
} else {
Log.d(LOGGING_TAG, "null response (result.pdf)")
eventSinkPDF?.success(null)
}
}
}
}
The exception that is thrown is the following:
E/GmsDocScanDelAct( 5777): Failed to handle scanning result
E/GmsDocScanDelAct( 5777): java.lang.IllegalStateException: Failed to handle result
E/GmsDocScanDelAct( 5777): at com.google.mlkit.vision.documentscanner.internal.zzg.run(com.google.android.gms:play-services-mlkit-document-scanner@@16.0.0-beta1:12)
E/GmsDocScanDelAct( 5777): at android.os.Handler.handleCallback(Handler.java:938)
E/GmsDocScanDelAct( 5777): at android.os.Handler.dispatchMessage(Handler.java:99)
E/GmsDocScanDelAct( 5777): at android.os.Looper.loopOnce(Looper.java:210)
E/GmsDocScanDelAct( 5777): at android.os.Looper.loop(Looper.java:299)
E/GmsDocScanDelAct( 5777): at android.os.HandlerThread.run(HandlerThread.java:67)
No logs indicate that activity was destroyed/no results were returned. I wonder if there's any other way to reliably predict this IllegalStateException.