我使用 flutter-darts 创建了 QR码生成应用程序。除了共享部分,其他一切都很好。我使用以下代码共享了我生成的 png图像。
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());
}
}
当我尝试使用上述功能共享生成的图像时,发生了异常,
我应该怎么做才能解决这个问题。我认为这将由于channel参数而发生。
最佳答案
我做了类似的事情,发现Plugin esys_flutter_share:
带来了调用跨平台共享的最佳方法。
试试这个代码片段,这对我有用:
await Share.file(
'Export', 'export.png', file.readAsBytesSync(), 'export/png');
关于image - 无法使用Flutter channel 共享文件-缺少插件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60630173/