Download image from blob url - flutter

I need to download an image from a blob url.
I try using those solution, but none of them worked for me:
Flutter WebView blob pdf download
https://github.com/guoguoguilai/flutter-webview-blob-download
Display a bloc URL image in flutter)
final response = await get(
Uri.parse(blob),
);
final documentDirectory = await getApplicationDocumentsDirectory();
final File file = File(join(documentDirectory.path, 'image.png'))
..writeAsBytesSync(response.bodyBytes);
Does anyone have a possible solution ?

Related

Flutter: How to generate screenshots in arbitrary resolutions?

In Flutter, how do you get a screenshot for an arbitrary screen resolution? I.e. for a screen resolution that's different from the resolution the app is running in?
Is there a way to tell the Flutter engine to paint a single frame into a buffer with a specific resolution?
What I'm trying to accomplish is to generate the screenshots for the various app stores from within the app, but without scaling and cropping screenshots that were taken for different devices/resolutions.
How would you tackle that requirement?
Any advise is welcome,
Thank you!
if you are talking about taking a screenshot of a particular widget and saving it
the you have to use renderrepaint boundry with key .
dependencies - path provider
Image gallery saver ..
takeScreenshot(BuildContext context,GlobalKey _key,String slug) async {
RenderRepaintBoundary boundary = _key.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
final snackBar = SnackBar(content: Text("Image saved to gallery"));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
final directory = (await getApplicationDocumentsDirectory()).path;
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final result = await ImageGallerySaver.saveImage(
byteData.buffer.asUint8List(),
name: "name.jpg");
}

Flutter passing image string

i'm trying to send an image on instagram using flutter with the social_share package.
My issue is that i'm new with flutter and i don't find a proper way to send a path to the function SocialShare.shareInstagramStory(imageFile.path, "#ffffff", "#000000", "https://deep-link-url");
My images are located in the root of the flutter project in assets/images/my_images.
When i try to put the path SocialShare.shareInstagramStory("assets/images/my_image.jpeg");
i guess the phone doesn't know this path since it's looking on his own directory
hope i'm clear enough and thanks for your time!
Edit : here's my code:
onPressed: () async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = File(tempDir.path);
final byteData = await rootBundle.load('assets/images/img_1.jpeg');
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
await SocialShare.shareInstagramStory(file.path);
Navigator.of(context).pop();
},
i got this error :
E/flutter (12761): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.app/cache' (OS Error: Is a directory, errno = 21)
So basically, what you need to do is convert your asset image into a file, then create a temporary path and store this file there. Then you can reference that path
We need to create a temporary path with this plugin https://pub.dev/packages/path_provider
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Then create a file
File file = File(tempDir.path+'/img_1.jpeg');//You might have to play arounf with the /. Remove it or put it in the end to see what works
Now we need to write the image to this
final byteData = await rootBundle.load('assets/images/img_1.jpeg');
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
Now you can use this temporary path to do what you want

flutter path_provider could not read downloaded files

I am using path_provider on flutter and would like to build Download file(mp4&jpg, from aws S3) and play it. I succeeded download and confirmed file is downloaded.(on ios)
But, when access the files using path_provider, raised error like below.
Unable to load asset: /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers
the saving method is below
Future<File> _getFile(String filename) async {
final dir = await getApplicationDocumentsDirectory();
return File("${dir.path}/$filename");
}
final file = await _getFile("filename");
file.writeAsBytes(Bytes);
and the load method is below
final directory = await getApplicationDocumentsDirectory();
.
.
.
Image(image: AssetImage('${directory.path}/filename')) // /Users/username/Library/Developer/CoreSimulator/Devices/B78BC67E-B127-4DD0-8A42-965EE645157B/data/Containers/Data/Application/46EF23F5-840C-47D4-8314-B97CDEE2198C/Documents
.
.
.
Actually I need to use FileImage instead of Asset.
Thanks for GrahamD

How do you download an image from the internet onto a file in Flutter?

I'm making a Flutter project where an image is displayed in listview and the user can share, favourite or download the image. I'm stuck on the downloading part. Is it possible to save an image file onto the phone's storage for offline use?
What I want to happen is I can create and write an image file
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/$title.txt');
}
// Write an image by obtaining an image through a URL
and then be able to access it through
Image.file('$path/$title.txt')

how to embed widgets to images and save it to the device

I have an image with a container on it. I want to be able to edit the TextField inside the container and save the image as an image to the device.
My current setup is a stack where I have the image and a container on top of each other. How can I save that as an image?
Note that I don't want to save the whole screen, just the image and whatever on it.
Future<File> createWave(BuildContext context, GlobalKey screen) async {
RenderRepaintBoundary boundary = screen.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
final directory = (await getApplicationDocumentsDirectory()).path;
File imgFile = File('$directory/screenshot.png');
await imgFile.writeAsBytes(pngBytes);
return imgFile;
}
Thankfully since flutter uses skia to render it's graphics, we are able to take screenshots of widgets and there is already a really good article that does somethings similar. Basically you wrap your widget in a RepaintBoundary and assign it a key then use the key to call the RenderObject.toImage method. Here is another simple example to do this. It's amazing how simple it is :D