imageflutterdartpathsave-image

How to save captured or image that selected from gallery using image provider, in to assets folder?


I want to save image that captured from following function into app asset folder.

_openGallery(BuildContext context) async{
          var picture = await ImagePicker.pickImage(source: ImageSource.gallery);

          this.setState((){
            imageFile=picture;
          });
          Navigator.of(context).pop();
        }

        _openCamera(BuildContext context) async {
          var picture = await ImagePicker.pickImage(source: ImageSource.camera);
          this.setState((){
            imageFile=picture;
          });
          Navigator.of(context).pop();
        }

I've tried this code

Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path;
File newImage = await _image.copy('$path');

but it gives me this error OS Error: No such file or directory, errno = 2 flutter..Can someone help with this.


Solution

  • try giving name to your file

    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path+'/my_image.png';
    File f = File(path)
    f.writeAsBytes(yourImage.asBytes);