如何将 flutter 应用程序中的图像文件共享到其他应用程序

M A*_* Al 4 flutter

我有一个应用程序,该应用程序向用户显示一张图像。该图像我将其作为链接保存在 MySQL 数据库中,并将图像保存在服务器文件夹中。现在我尝试让用户可以从我的应用程序将该图像分享到其他应用程序,例如 WhatsApp 或 Facebook。

我使用 share_plus 3.0.5 包来实现:

分享加3.0.5

  await Share.shareFiles([//////////////////here/////////////], text: 'Image Shared');
Run Code Online (Sandbox Code Playgroud)

通过此代码获取图像:

  Future MakeShare() async {
var response = await http.get(
    Uri.parse("https://*********/ImageMakeShare.php?ID=" + widget.IDS.toString()),
    headers: {"Accept": "application/json"});

setState(() {

  var convertDataToJson = json.decode(response.body);
  dataImage = convertDataToJson['result'];
  if (dataImage != null) {

    imageMaine = dataImage[0]['image'];

}}); }
Run Code Online (Sandbox Code Playgroud)

我试着让它像那样

  await Share.shareFiles([imageMaine ], text: 'Image Shared');
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

E/flutter (10763): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(https:/*******0ee2e.png (No such file or directory), null, null, null)
Run Code Online (Sandbox Code Playgroud)

现在我需要知道如何让用户可以将该图像共享到其他应用程序。

有人可以帮助我吗?

Gab*_*che 10

正如您从文档 中看到的那样,shareFiles()函数需要一个指向设备上本地路径的字符串列表。您传递的是 URI (imageMaine),它不是本地路径,因此插件会抛出异常。

如果您想共享链接,那么您应该使用该share()功能。如果你想共享一个文件,你应该首先获取你的罚款,然后使用 shareFiles 函数发送它:

  final url = Uri.parse("myLink");
  final response = await http.get(url);
  await File('/yourPath/myItem.png').writeAsBytes(response.bodyBytes);

  await Share.shareFiles(['/yourPath/myItem.png'], text: 'Image Shared');
Run Code Online (Sandbox Code Playgroud)