0

I'm confused I've taken a picture but can't save it to the phone.How should I save pictures to the phone?

I used the following code to take a picture.

ValueNotifier<XFile?>_imageFile=ValueNotifier(null);

ImagePicker _imagePicker=ImagePicker();

Future<void> _getImage(ImageSource imageSource) async{
    XFile? imgFile=await _imagePicker.pickImage(source: imageSource);
    _imageFile.value=imgFile;
  }
Debby
  • 1
  • 1

2 Answers2

0

This is another aproach to save your picked image:

final XFile? pickedFile = await _imagePicker.pickImage(source: imageSource);
new File(pathToSaveHere).writeAsBytesSync(await pickedFile!.readAsBytes());

about "pathToSaveHere" you can use "path_provider" plugin of flutter https://pub.dev/packages/path_provider. It provide severeal methods to get paths you are able to save. Note that: due permissions restrictions you CANNOT save everywhere.

G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
0

First add gallery_saver package then call this function,

void _takePhoto() async {
    final _pickedImg= await ImagePicker.pickImage(source: 
       ImageSource.camera)
         .then((File recordedImage) {
            // saving the picked Image to Gallery
            if (recordedImage != null && recordedImage.path != null) {
            GallerySaver.saveImage(recordedImage.path);
            setState(() {});
      }});
   // ur picked image in File form 
   File? img = File(_pickedImg!.path);
  }
JB Jason
  • 291
  • 2
  • 8
  • I'm confused what pickedImageFile is. It appears in error when I type it inside my vscode.Also,yes, i wanna save the pic to the Gallery.How should i use "gallery_saver" package? – Debby Feb 22 '23 at 02:12
  • ohh um sorry i couldn't edit. its File? _pickedImage = File(img!.path) . Search on youtube, "how to save image to gallery in flutter" – JB Jason Feb 22 '23 at 05:59
  • I'm still confused about saving pictures to gallery. The examples I found are all to save a specific picture, but I want to save the picture I just took with image_picker to the gallery. – Debby Feb 22 '23 at 08:12
  • Got it. updated my comment. check it out – JB Jason Feb 22 '23 at 09:56
  • Thank you for your answer. But I got the following error message:The argument type 'Null Function(File)' can't be assigned to the parameter type 'FutureOr Function(XFile?)'.How should I solve it. – Debby Mar 03 '23 at 08:04