Does anyone know how to solve this error when converting a ByteData variable to BitMap?
I am building a Flutter Application, and there is one part in my code that I need to use an Android API (written in Kotlin). Specifically, I’m using a Platform Channel to pass an Image as an argument in a method in the Kotlin file. Based on this site, I figured out that I need to get the ByteData from the image File and send them as a parameter to the function, where the Kotlin code will convert them to BitMap. However, where I get the following error:
E/flutter (14160): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Invalid argument: TypedDataView(cid: 142)
E/flutter (14160): #0 StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:403:7)
E/flutter (14160): #1 StandardMethodCodec.encodeMethodCall (package:flutter/src/services/message_codecs.dart:535:18)
E/flutter (14160): #2 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:150:13)
E/flutter (14160): #3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
E/flutter (14160): #4 _MyHomePageState.build.readTextKotlin (package:smux_demo/scan_activity.dart:1033:41)
E/flutter (14160): <asynchronous suspension>
E/flutter (14160):
I/art (14160): Background sticky concurrent mark sweep GC freed 285618(7MB) AllocSpace objects, 0(0B) LOS objects, 65% free, 3MB/11MB, paused 1.846ms total 143.990ms
Does anyone know how to solve this?
Code in Dart:
Future readTextKotlin() **async** {
**for** (int countReceiptImages = 0; countReceiptImages < **listProducts** . **length** ; countReceiptImages++) {
File file = File( **listProducts** [countReceiptImages]);
**final** bytes = **await** file.readAsBytes(); <i>// Uint8List</i> **final** byteData = bytes. **buffer** .asByteData(); <i>// ByteData</i> **var** sharedData = **await** *platform* .invokeMethod( **"getSharedValue"** , byteData);
}
}
Code in Kotlin:
class MainActivity: FlutterActivity() {
private val DATA_CHANNEL = "app.channel.shared.data"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), DATA_CHANNEL).setMethodCallHandler { call, result ->
if (call.method!!.contentEquals("getSharedValue")) {
var argData = call.arguments
val arrayInputStream = ByteArrayInputStream(argData as ByteArray?)
val bitmap = BitmapFactory.decodeStream(arrayInputStream)
}
}
}
}