Flutter, ImagePicker giving different paths every time for the same image

Viewed 21

In my app, I am picking a photo from gallery and save it's path with ImagePicker. Then, I am showing it with the path I saved. The problem is, ImagePicker is giving me a different path every time even if I choose the same picture again so I can not open the image with the path I saved, it's giving the error no such file. How I am getting the path is:

onPressed: () async {
              final XFile? image =
                  await imagePicker.pickImage(source: ImageSource.gallery);
              if (image != null) {
                setState(() {
                  imagePath = image.path;
                });
              }
            },

It gives me a path like this: "/private/var/mobile/Containers/Data/Application/..." so on. How can I get the actual path and open it?

1 Answers

ImagePicker will create a copy of the image to your app's cache directory, thus you need to move it into a more stable location.

It also has no knowledge of what was picked before, thus it will create a new image every time you pick it. If you need to have a stable file path based on the image, perhaps give a try to file_picker.

Related