Image Picker Flutter - Pick Image From Gallery or Camera

Written by mouli150388 on Jan 24th, 2022 Views Report Post

If we are going to create a Flutter application that is accessing the camera to take a picture or using the gallery to pick an image then in this tutorial we are going to learn how to access Camera or Gallery in a flutter application. Pick Image from gallery Flutter

In this Image Picker Example, we are going to use ImagePicker dependency to access the camera/gallery. You can read also How to Capture Video from camera in flutter

dev_dependencies:
  image_picker: ^0.8.3+3

How to capture Image from Camera?

To capture an image from the camera first we need to create ImagePicker instance

This ImagePicker contains method getImage() will return Image File.

To get from Camera we need to pass the ImageSource.camera parameter to this getImage().

void _openCamera(BuildContext context)  async{
    final pickedFile = await ImagePicker().getImage(
          source: ImageSource.camera ,
          );
          setState(() {
          imageFile = pickedFile!;
    });
    Navigator.pop(context);
}```

we don't know how much time will it take to capture the image from the camera, so we are written this method inside the future method. 

similarly to pick an image from the gallery we need to pass ImageSource.gallery as a parameter.

```java
void _openGallery(BuildContext context) async{
  final pickedFile = await ImagePicker().getImage(
    source: ImageSource.gallery ,
  );
  setState(() {
    imageFile = pickedFile!;
  });

  Navigator.pop(context);
}

Download complete code for Pick Image from Gallery in flutter using Image Picker


Comments (0)