Sub topic 2
Resizing, quality, multiple images, and video
This chapter assumes you know Dart async/await, can add packages in pubspec.yaml, and have set up image_picker and camera as described earlier. You should also be comfortable with initState and dispose in a StatefulWidget.
The examples use a shared picker:
final ImagePicker picker = ImagePicker();Resizing and compressing
pickImage accepts maxWidth, maxHeight, and imageQuality (0 to 100). Smaller values produce smaller files, which upload faster.
final XFile? file = await picker.pickImage(
source: ImageSource.gallery,
maxWidth: 1024,
imageQuality: 85,
);> Note: Resizing happens inside the plugin. Check the output size on a real device, because platforms differ in how they apply imageQuality.
Multiple images and video
pickMultiImage returns a List<XFile>. pickVideo picks one video and accepts maxDuration.
final List<XFile> photos = await picker.pickMultiImage(imageQuality: 85);
final XFile? clip = await picker.pickVideo(
source: ImageSource.gallery,
maxDuration: const Duration(seconds: 30),
);Showing, saving, and uploading the result
An XFile has path and name. Show a mobile file with Image.file(File(file.path)). Picked files live in temporary storage, so copy any file you need to keep into the app documents directory.
To upload, read the bytes and send them as multipart form data:
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
Future<void> uploadPhoto(XFile file) async {
final Uint8List bytes = await file.readAsBytes();
final request = http.MultipartRequest(
'POST',
Uri.parse('https://example.com/upload'),
)..files.add(
http.MultipartFile.fromBytes('photo', bytes, filename: file.name),
);
final response = await request.send();
debugPrint('Upload status: ${response.statusCode}');
}Recovering from Android activity destruction
Android can destroy your activity while the gallery or camera app is open, for example when memory runs low. When the user returns, the result may be lost. Call retrieveLostData once at startup to recover it.
Future<List<XFile>> recoverFiles() async {
final LostDataResponse response = await picker.retrieveLostData();
if (response.isEmpty) return [];
return response.files ?? [];
}Listing the device cameras
availableCameras() returns a List<CameraDescription>. Each entry has a lensDirection, which tells you whether it is a back, front, or external camera.
import 'package:camera/camera.dart';
Future<CameraDescription?> findBackCamera() async {
final List<CameraDescription> cameras = await availableCameras();
for (final camera in cameras) {
if (camera.lensDirection == CameraLensDirection.back) return camera;
}
return cameras.isEmpty ? null : cameras.first;
}Initializing a CameraController
Create a CameraController with a camera and a resolution, then await initialize(). Set enableAudio: false if you do not record sound.
Future<CameraController> startCamera(CameraDescription camera) async {
final controller = CameraController(
camera,
ResolutionPreset.medium,
enableAudio: false,
);
await controller.initialize();
return controller;
}Displaying the live preview
After initialization, CameraPreview(controller) shows the live feed.
Exercise
Why should the app call retrieveLostData at startup on Android?
Show answer
Android can destroy the activity while the picker is open. Without retrieveLostData, the picked file is lost when the user returns to the app.