arbiter
arbiter

Reputation: 451

Saving Flutter image from gallery with ImagePicker

I have this function to save image from gallery and display it as a background:

Future _getImage() async {
// ImagePicker picker = ImagePicker();

PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);

if (pickedFile == null) return;
File tmpFile = File(pickedFile.path);
tmpFile = await tmpFile.copy(tmpFile.path);

setState(() {
  if (pickedFile != null) {
    //_image = File(pickedFile.path);
    _image = tmpFile;
    print('_image: $_image');
  } else {
    print('No image selected');
  }
});

}

but after I invoke it, it says that the image is empty:

File: '/data/user/0/com.app.flutter/cache/image_picker5156338879856055740.jpg' is empty and cannot be loaded as an image.

I'm not sure what's happening here, since it prints the image in the console fine like this:

_image: File: '/data/user/0/com.app.flutter/cache/image_picker5156338879856055740.jpg

so it means that it is in the cache, but for some reason it returns as empty and won't add the image to the background:

 decoration: BoxDecoration(
      image: DecorationImage(
        image: _image == null
            ? MemoryImage(kTransparentImage)
            : FileImage(_image),
        fit: BoxFit.cover,

added the build just in case there is an error there, but I don't think there is. Basically I just need the image from gallery to be persistent and stay in the app.

EDIT: Added the setState() to the code, but it is still not persistent, there must be something I'm missing:

Future _getImage() async {
// ImagePicker picker = ImagePicker();

PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);

if (pickedFile == null) {
  return null;
}

Directory appDirectory = await getApplicationDocumentsDirectory();
File newImage = File(appDirectory.path + 'fileName');
newImage.writeAsBytes(File(pickedFile.path).readAsBytesSync());

setState(() {
  _image = newImage;
  print(newImage.path + ' test');
});

}

Upvotes: 0

Views: 602

Answers (2)

arbiter
arbiter

Reputation: 451

Nevermind, I managed to make it persistent with SharedPreferences, I simply made two different functions, each to save or load the image, and added _loadImage() to initState

void _saveImage(path) async {
SharedPreferences saveImage = await SharedPreferences.getInstance();
saveImage.setString('imagepath', path);
print('Image Saved!');



  void _loadImage() async {
SharedPreferences saveImage = await SharedPreferences.getInstance();
setState(() {
  _imagepath = saveImage.getString('imagepath');
});

}

Upvotes: 0

Ishanga Vidusha
Ishanga Vidusha

Reputation: 304

Try this

Future _getImage() async {
  ImagePicker picker = ImagePicker();
  final pickedFile = await picker.getImage(source: ImageSource.gallery);
  if (pickedFile == null) {
    return null;
  }
  Directory appDirectory = await getApplicationDocumentsDirectory();
  File newImage = File(appDirectory.path + 'fileName');
  await newImage.writeAsBytes(File(pickedFile.path).readAsBytesSync());

  setState(() {
    _image = newImage;
  });
}

Upvotes: 2

Related Questions