'How to Share Image from API(URL) in flutter?
Here I want to share image which I get through API. I tried different method for this functionality but I did not get any solution because every solutions have for only one image.
Exactly, I get multiple image from Url and I open any particular image in next page. So, I want to share that image which I opened in another page.
I tried for sharing image but I could not did this. Whenever I try to share that image, Image url share with sharing option on device but I want share image not URl of image How I can accomplish this?
Solution 1:[1]
You can use the esys_flutter_share plugin.
Install it, and then get dependencies:
dependencies: esys_flutter_share: ^1.0.2Import the plugin to your code:
import 'package:esys_flutter_share/esys_flutter_share.dart';Use the below code:
var request = await HttpClient().getUrl(Uri.parse('https://yourImageURL.jpg')); var response = await request.close(); Uint8List bytes = await consolidateHttpClientResponseBytes(response); await Share.file('ESYS AMLOG', 'amlog.jpg', bytes, 'image/jpg');
Solution 2:[2]
You have to download the image like this:
http.Response response = await http.get(url);
then create an image on the device using the downloaded image like this (Read and Write Files):
final directory = await getTemporaryDirectory();
final path = directory.path;
final file = File('$path/image.png');
file.writeAsBytes(response.bodyBytes);
The getTemporaryDirectory() is in the plugin path_provider. Now, you have the image you'd like to share stored in the temporarily as "image.png" and you can use the share_plus plugin to share the image like this:
Share.shareFiles(['$path/image.png']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cody Gray |
| Solution 2 | Abdulrazak Zakieh |
