Sub topic 2
Configuring Android and iOS
This chapter assumes you know Dart async and await, Flutter widget lifecycles, pubspec.yaml, and the basic Android and iOS project folders. You will configure two plugins:
image_pickeropens a system gallery or camera app.cameragives your Flutter UI a live preview and direct camera controls.
Add the Flutter packages
Add the packages to pubspec.yaml, then fetch them:
dependencies:
flutter:
sdk: flutter
image_picker: ^1.1.2
camera: ^0.11.0+2Run this command from the project directory:
flutter pub getUse the versions that match your project and the current package releases. If a package requires a newer Android SDK or iOS deployment target, update the platform project before writing camera code.
Android configuration
The camera plugin needs permission to use the device camera. Add this declaration inside the <manifest> element in android/app/src/main/AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA" />
<application
android:label="media_demo"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- Existing application configuration -->
</application>
</manifest>The image_picker plugin usually uses Android's system picker or camera activity. Do not add broad storage permissions unless your app has a separate, documented need to read shared files directly. Unnecessary storage permissions increase review and privacy problems.
Android can destroy the activity while another app, such as the gallery, is open. Therefore, picker code should recover lost data when the activity returns. The next chapter that covers picker basics will show the recovery API.
iOS configuration
iOS requires a usage description for each protected resource. Add the keys inside ios/Runner/Info.plist:
<key>NSCameraUsageDescription</key>
<string>This app uses the camera to take photos and videos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app lets you choose photos and videos.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app records audio with video.</string>Use NSMicrophoneUsageDescription only when your feature records audio, such as video recording. The text should explain the feature in terms a user can understand. If a required key is missing, iOS can deny access or terminate the operation.
Which permission belongs to which feature?
| Feature | Android setup | iOS usage description |
|---|---|---|
| Choose a photo | Usually system picker handling | `NSPhotoLibraryUsageDescription` |
| Take a photo | `CAMERA` permission | `NSCameraUsageDescription` |
| Record video with sound | Camera and microphone access | Camera and microphone descriptions |
A permission declaration does not guarantee access. The user can still deny permission, and the device may have no camera. Your application must handle both cases.
Verify the configuration
Run static checks and launch the app on both platforms:
flutter analyze
flutter runTest the first launch, a denied permission, and a later launch after permission changes in system settings.
Exercise
Which iOS usage descriptions are needed for taking a silent photo, and which additional description is needed for video with sound?
Show answer
A silent photo needs NSCameraUsageDescription. Video with sound also needs NSMicrophoneUsageDescription. Choosing media from the library uses NSPhotoLibraryUsageDescription.