Dani
Dani

Reputation: 4186

White/blank screen after close camera in Flutter

I have a widget to take pictures on my app working like so:

final File imageFile =
        await ImagePicker.pickImage(source: ImageSource.camera).then((test) {
      print('TEST $test');
      return test;
    });

I can open the camera without any error and also take the picture but when I try to go back or accept the picture I've taken the app displays an white screen and the console show no errors at all.

This is failing on a real device (Xiaomi Redmi Note 8t) but it works on Android Emulator.

The only message I can see is Lost connection to device. when I take the camera

Upvotes: 4

Views: 2619

Answers (2)

Johan
Johan

Reputation: 1

I found the solution for flutter 2

In Android

You need to add this to your 3 manifest (debug, main , profile)

<uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

Then pick the image with a try catch, like this

Future getbehind() async {
    try{
      final pickedFile = await picker.getImage(source: (ImageSource.camera:ImageSource.gallery),)
          .then((value) {
        setState(() {
          if (value != null) {
            behind = File(value.path);
          } else {
            print('No image selected.');
          }
        });
      });


    }catch(e){

    }

Run the app in a release mode and its done!

Upvotes: 0

Dani
Dani

Reputation: 4186

Fixed adding a try catch:

Future<Null> _pickImageFromCamera(BuildContext context, int index) async {
    File imageFile;
    try {
      imageFile = await ImagePicker.pickImage(source: ImageSource.camera)
      .then((picture) {
        return picture; // I found this .then necessary
      });
    } catch (eror) {
      print('error taking picture ${error.toString()}');
    }
    setState(() => this._imageFile = imageFile);
  }

Upvotes: 1

Related Questions