Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Save Image File in Flutter ? File selected using Image_picker plugin

Tags:

flutter

dart

I am really confused. Flutter is awesome but some time is stuck the mind

All the code are done. selected file also showing in preview but I try to save that file in local android storage. I can't get success in

  Future getImage(ImageSource imageSource) async {     var image = await ImagePicker.pickImage(source: imageSource);      setState(() {       _image = image;     });   }  

Select file using this code and my file in _image now I try to store using path_provider and dart.io but I can't get save methodology.

like image 613
Ravindra Bhanderi Avatar asked Jul 14 '18 11:07

Ravindra Bhanderi


People also ask

How do I store image path in flutter?

// using your method of getting an image final File image = await ImagePicker. pickImage(source: imageSource); // getting a directory path for saving final String path = await getApplicationDocumentsDirectory(). path; // copy the file to a new path final File newImage = await image. copy('$path/image1.

How do I save images in Sharedpreferences in flutter?

You can convert the Image to Unit8List then convert UnitList8 to base64 and save it. Here is an example. Then use it. Show activity on this post.


1 Answers

Using await ImagePicker.pickImage(...), you are already on the right track because the function returns a File.

The File class has a copy method, which you can use to copy the file (which is already saved on disk by either the camera or by lying in gallery) and put it into your application documents directory:

// using your method of getting an image final File image = await ImagePicker.pickImage(source: imageSource);  // getting a directory path for saving final String path = await getApplicationDocumentsDirectory().path;  // copy the file to a new path final File newImage = await image.copy('$path/image1.png');  setState(() {   _image = newImage; }); 

You should also note that you can get the path of the image file from ImagePicker using image.path, which will also contain the file ending that you might want to extract and you can save your image path by using newImage.path.

like image 173
creativecreatorormaybenot Avatar answered Sep 17 '22 23:09

creativecreatorormaybenot