Retrieve image routes in Flutter

I would like to retrieve an image from gallery using the image_picker package and then store the images route in a database. After that I would like to retrieve the route from db and show the image.

Tn short, this is what my code so far does:

  File _image;

  Future getGalleryImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

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

The method above gets the image from the users gallery, and then

        new IconButton(icon: Icon(Icons.add_a_photo), onPressed: (){
          getGalleryImage();
        }),
        new Container(
          child: _image == null
              ? new Container()
              : new Image.file(_image),
        ),

displays the image.

I would now like to know 1) how to retrieve a string with the route to the selected image and 2) how to display the image based on the route string. Any help would be highly appreciated since I am kind of stuck on this issue.

1 answer

  • answered 2018-11-08 15:23 Günter Zöchbauer

    ImagePicker returns a File and you get the path using the Files path getter.

    final file = await ImagePicker.pickImage(source: ImageSource.gallery);
    print(file.path);
    

    You can then later create a new File instance with the retrieved path

    final file = File(path);
    

    and display it using for example

    Image.file(file);