Reputation: 922
I created QR code generating app using flutter-darts. Everything is fine except sharing part. I used following code to share my generated png image.
Future<Null> _captureAndSharePng() async {
try {
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
var image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
final tempDir = await getTemporaryDirectory();
final file = await new File('${tempDir.path}/image.png').create();
await file.writeAsBytes(pngBytes);
/*final channel = const MethodChannel('channel:me.alfian.share/share');
assert(image != null);
return channel.invokeMethod('shareImage', image);*/
final channel = const MethodChannel('channel:me.alfian.share/share');
channel.invokeMethod('shareFile', 'image.png');
} catch (e) {
print(e.toString());
}
}
When I am trying to share my generated image using above function, an Exception is occuring,
What should I do to fix this. I think this will happen because of the channel parameter.
Upvotes: 0
Views: 510
Reputation: 905
You need to modify MainActivity
class also
Follow this answer: https://stackoverflow.com/a/50007287/13617136
Upvotes: 0
Reputation: 182
I have done something similar and found out that the Plugin esys_flutter_share:
brings the best method to invoke cross paltform sharing.
Try this code snippet, this works for me:
await Share.file(
'Export', 'export.png', file.readAsBytesSync(), 'export/png');
Upvotes: 1