Sub topic 5
Resizing, quality, multiple images, and video
Prerequisites: you should know Dart async and await, Flutter widgets, and how to add a package in pubspec.yaml. This chapter assumes that image_picker is already installed and platform permissions are configured.
Resize images before your app uses them
The pickImage method accepts optional size and quality settings:
maxWidthlimits the image width in pixels.maxHeightlimits the image height in pixels.imageQualityis a value from 0 to 100. Higher values usually produce better quality and larger files.- The picker preserves the image aspect ratio. A maximum is not an exact output size.
These options reduce memory use and upload time. They are useful for profile pictures, messages, and forms where the original camera image is unnecessarily large.
import 'package:image_picker/image_picker.dart';
final ImagePicker picker = ImagePicker();
Future<XFile?> chooseProfilePhoto() {
return picker.pickImage(
source: ImageSource.gallery,
maxWidth: 1024,
maxHeight: 1024,
imageQuality: 85,
);
}The returned file may be smaller than the original, but the exact dimensions and file size depend on the source image and the platform implementation.
Select several images
Use pickMultiImage when a user can choose more than one image. It returns a list. An empty list means that the user selected nothing or canceled the picker.
Future<List<XFile>> chooseAttachments() {
return picker.pickMultiImage(
maxWidth: 1600,
imageQuality: 80,
limit: 10,
);
}The limit option is supported on platforms that can enforce a selection limit. Your app should still check the returned list because platform behavior can differ.
A useful flow is:
- Open the picker.
- Check whether the list is empty.
- Display a preview for each
XFile. - Upload only after the user confirms.
- Show an error for files that fail validation or upload.
Pick a video
Use pickVideo for a video from the gallery or a recording from the system camera. The maxDuration option limits recording duration when the camera is used. It does not provide general video compression.
Future<XFile?> chooseVideo() {
return picker.pickVideo(
source: ImageSource.gallery,
maxDuration: const Duration(minutes: 2),
);
}
Future<XFile?> recordShortVideo() {
return picker.pickVideo(
source: ImageSource.camera,
maxDuration: const Duration(seconds: 30),
);
}imageQuality applies to images, not videos. If video size or bitrate must be controlled, use a dedicated video-processing solution after selection, and account for the extra processing time and temporary storage.
Choose settings deliberately
| Feature | Option | Main trade-off |
|---|---|---|
| Image dimensions | `maxWidth`, `maxHeight` | Smaller files versus less detail |
| Image compression | `imageQuality` | Smaller files versus visible artifacts |
| Multiple selection | `pickMultiImage` | More convenient selection versus more memory and upload work |
| Video duration | `maxDuration` | Shorter recordings versus less captured content |
Exercise
Which setting should you use to reduce a profile photo's upload size while keeping its proportions?
Show answer
Set maxWidth and maxHeight to reasonable limits and set imageQuality to a value such as 80 or 85. The picker keeps the aspect ratio.