简体   繁体   中英

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!),
            )),
    ),

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM