Back to Articles

Beginner

Camera and Gallery Integration in Flutter (image_picker)

· 6 min read

What Is image_picker?

image_picker is one of Flutter's official packages: it opens the device's native camera app or gallery picker and returns the file the user selected to you as an XFile. Instead of writing your own camera UI from scratch, it uses the system interface users are already familiar with.

Basic Usage: Camera and Gallery

The pickImage method on the ImagePicker class decides whether the camera or the gallery opens, via the source parameter.

final ImagePicker _secici = ImagePicker();

Future<void> kameradanCek() async {
  final XFile? dosya = await _secici.pickImage(source: ImageSource.camera);
  if (dosya != null) {
    setState(() => _secilenDosya = File(dosya.path));
  }
}

Future<void> galeridenSec() async {
  final XFile? dosya = await _secici.pickImage(source: ImageSource.gallery);
  if (dosya != null) {
    setState(() => _secilenDosya = File(dosya.path));
  }
}

Displaying the Selected File

An XFile's path can be turned into a File and shown directly with Image.file. When the user picks nothing (cancels), pickImage returns null — always check for that case.

_secilenDosya != null
    ? Image.file(_secilenDosya!, height: 200, fit: BoxFit.cover)
    : const Icon(Icons.image_outlined, size: 80, color: Colors.grey);

Limiting File Size

Photos taken with the camera can run several MB each; uploading that straight to a server is slow and costly. The maxWidth, maxHeight and imageQuality parameters resize and compress the file as it's read from the device.

final XFile? dosya = await _secici.pickImage(
  source: ImageSource.camera,
  maxWidth: 1280,
  imageQuality: 75, // 0-100 arası, JPEG sıkıştırma kalitesi
);

Picking Multiple Images

To pick more than one image, but only from the gallery, you use pickMultiImage; it returns a List<XFile>. The camera doesn't support multi-selection, since only one photo can be taken at a time.

Future<void> coklu secim() async {
  final List<XFile> dosyalar = await _secici.pickMultiImage(
    maxWidth: 1280,
    imageQuality: 75,
  );
  setState(() => _secilenDosyalar = dosyalar);
}

Things to Watch Out For

1. Permissions Are Requested Automatically, But Manifest/Info.plist Setup Is Required

image_picker triggers the runtime permission request (camera/gallery access) itself, but you still need to add the camera permission to AndroidManifest.xml on Android, and the NSCameraUsageDescription and NSPhotoLibraryUsageDescription descriptions to Info.plist on iOS. Missing these descriptions causes an unexplained crash on iOS.

2. When the User Denies Permission

If the user has permanently denied camera or gallery access, pickImage either silently returns null or throws a PlatformException; it's good practice to catch this and show a message directing the user to the settings screen.

Conclusion

image_picker brings camera and gallery access to your app with just a few lines of code. Choose the source with ImageSource, keep file size under control with maxWidth/imageQuality, use pickMultiImage for multi-selection, and fully define the platform permission texts (Info.plist, AndroidManifest.xml).