如何在flutter中从API(URL)共享图像?

J M*_*iya 4 dart flutter flutter-dependencies flutter-layout

这里我想把share image它打通API。我尝试了不同的方法来实现此功能,但没有得到任何解决方案,因为每个解决方案仅适用于一张图像。

确切地说,我从中获取了多个图像,然后在下一页中Url打开任何特定图像。image所以,我想要我在另一个页面中打开的share内容image

我尝试过,sharing image但我做不到。每当我尝试share这样做时imageImage url sharesharing可以选择device,但我share image不想要我URl如何image才能做到这一点?

Abd*_*ieh 5

您必须像这样下载图像:

http.Response response = await http.get(url);
Run Code Online (Sandbox Code Playgroud)

然后使用下载的图像在设备上创建图像,如下所示(读取和写入文件):

final directory = await getTemporaryDirectory();
final path = directory.path;
final file = File('$path/image.png');
file.writeAsBytes(response.bodyBytes);
Run Code Online (Sandbox Code Playgroud)

getTemporaryDirectory() 位于插件path_provider中。现在,您将要共享的图像临时存储为“image.png”,您可以使用share_plus插件来共享图像,如下所示:

Share.shareFiles(['$path/image.png']);
Run Code Online (Sandbox Code Playgroud)