首页 > 解决方案 > Flutter 如何通过分享插件分享文件图片

问题描述

Hello and thank you in advance!

我正在处理图像编辑。我可以从图像选择器中选择图像并在 drawstringdrawstring 的帮助下重写它。我可以将图像保存在我的文档目录中。我想通过 Share 插件从 documentDirectory 路径分享我的图像。但是,我收到一个错误。

代码和错误详情如下所示。

代码:

  FlatButton(
  child: Text("Apply Watermark Over Image"),
  onPressed: () async {
    final image1 = ui.decodeImage(_originalImage.readAsBytesSync());

    ui.drawString(image1, ui.arial_24, 300, 400, 'Hello And thank you');


    final documentDirectory = await getApplicationDocumentsDirectory();

    final file = new File(p.join(documentDirectory.path, "merged_image.jpg"));
   
    file.writeAsBytesSync(ui.encodeJpg(image1));
      // I HAVE THE IMAGE IN - documentDirectory.path "HERE I CANT SHARE THAT IMAGE" 
    final ByteData bytes = await rootBundle.load(documentDirectory.path);
    await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png', 
    text: 'My optional text.');
  },
)

我收到此错误:

[错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:无法加载资产:/data/user/0/com.photogranth.watermark/app_flutter

标签: fluttersharefile-sharing

解决方案


对于共享文件图像,您可以使用这个flutter_share_file

  FlatButton(
  onPressed: () async {
                Directory dir = await getApplicationDocumentsDirectory();
                File testFile = new File("${dir.path}/image.png");
                FlutterShareFile.shareImage(dir.path, "image.png", ShareFileType.image);
              },
)

推荐阅读