'Flutter image_picker crashes when opening camera
I'm trying to use image_picker
on Flutter and it works perfectly on Android emulator. But when I try to use it on my real device, it crashes when I clicking the button to open the camera without giving any error. How can I solve it? My phone is an Android (Xiaomi Mi 8).
My code:
File? _image;
final imagePicker = ImagePicker();
Future getImage() async {
final image = await imagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = File(image!.path);
});
}
ElevatedButton(
onPressed: getImage,
child: Text("Foto"),
),
Container(
height: 300,
width: 300,
decoration: BoxDecoration(
color: Colors.red,
),
child: _image == null
? Text("No image selected")
: CircleAvatar(
backgroundImage: FileImage(_image!),
)),
),
Solution 1:[1]
You have to add permission for accessing images. For IOS
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app does not require access to the microphone.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>
For android
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |