Reputation: 2284
in my flutter app I am displaying an image from the internet using CachedNetworkImage
.
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),
I would like now to be able to share that cached image with another app, for example instagram. How can I identify the cached image and pass it as an argument for my share function. See below my code where you see my share function using WcFlutterShare
which takes a ByteData
as source. I would like to replace assets/images/logo.png with the cachedImage
FlatButton(
onPressed: () {
final ByteData bytes = await rootBundle.load('assets/images/logo.png');
await WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.png',
mimeType: 'image/png',
bytesOfFile: bytes.buffer.asUint8List());
},
child: Text(
"Share ",
),
)
Note my current solution is as follow but I am downloading the image 2 times...
http.Response response = await http.get("http://via.placeholder.com/350x150");
final bytes = response.bodyBytes;
await WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.jpeg',
mimeType: 'image/jpeg',
bytesOfFile: bytes);
Upvotes: 1
Views: 1907
Reputation: 9038
You can pass your own cacheManager
to CachedNetworkImage which will allow you to retrive cached file using getFile
(docs). Then you can retrieve the byte stream.
final BaseCacheManager baseCacheManager = DefaultCacheManager();
...
@override
Widget build(BuildContext context) {
baseCacheManager
.getFile("http://via.placeholder.com/350x150")
.listen((info) {
log(info.file.path);
log(info.originalUrl);
});
return CachedNetworkImage(
cacheManager: baseCacheManager,
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
);
}
This is a button I made to test my idea:
FlatButton(
child: Text("Share"),
onPressed: () {
baseCacheManager
.getFile("http://via.placeholder.com/350x150")
.first
.then((info) {
info.file.readAsBytes().then((bytes) {
WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.jpeg',
mimeType: 'image/jpeg',
bytesOfFile: bytes);
});
});
}),
Upvotes: 3