4

I am using flutter image_picker: ^0.6.0+17 to get image for a product in app it absolutely works fine on emulator, but on real android device the app crashes and restarts when i click 'open camera' button.

i have tried to use the emulator as same api level as my device i am testing it on redmi 6A android oreo 8.1 no problem is found flutter doctor or flutter analyze selecting image from gallary works fine for both .

import 'dart:io';

import 'package:firstapp/models/product.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImageInput extends StatefulWidget {
  final Function setImage;
  final Product product;

  ImageInput(this.setImage, this.product);

  @override
  State<StatefulWidget> createState() {
    return _ImageInputState();
  }
}

class _ImageInputState extends State<ImageInput> {
  File _imageFile;

  Future _getImage(BuildContext context, ImageSource source) async {
    print('getting image');
    File image = await ImagePicker.pickImage(source: source, maxWidth: 600);
    if(image == null){
      return null;
    }
    setState(() {
      print('file = image');
      _imageFile = image;
    });
    print('setting image');
    widget.setImage(image);
    Navigator.pop(context);
  }

  void _openImagePicker(BuildContext context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            padding: EdgeInsets.all(10),
            height: 150,
            child: Column(children: <Widget>[
              Text(
                'Pick an Image',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(
                height: 10,
              ),
              FlatButton(
                textColor: Theme.of(context).primaryColor,
                child: Text('Use Camera'),
                onPressed: () {
                  _getImage(context, ImageSource.camera);
                },
              ),
              FlatButton(
                textColor: Theme.of(context).primaryColor,
                child: Text('Use Gallery'),
                onPressed: () {
                  _getImage(context, ImageSource.gallery);
                },
              )
            ]),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    final buttonColor = Theme.of(context).primaryColor;
    Widget previewImage = Text('Please select an image.');
    if (_imageFile != null) {
      previewImage = Image.file(
        _imageFile,
        height: 300.0,
        width: MediaQuery.of(context).size.width,
        fit: BoxFit.cover,
        alignment: Alignment.center,
      );
    } else if (widget.product != null) {
      previewImage = Image.network(
        widget.product.image,
        height: 300.0,
        width: MediaQuery.of(context).size.width,
        fit: BoxFit.cover,
        alignment: Alignment.center,
      );
    }

    return Column(
      children: <Widget>[
        OutlineButton(
          onPressed: () {
            _openImagePicker(context);
          },
          borderSide: BorderSide(color: buttonColor, width: 1),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(
                Icons.camera_alt,
                color: buttonColor,
              ),
              SizedBox(
                width: 5.0,
              ),
              Text(
                'Add Image',
                style: TextStyle(color: buttonColor),
              )
            ],
          ),
        ),
        SizedBox(
          height: 10,
        ),
        previewImage,
      ],
    );
  }
}

these are the debug log after i press use camera and then app restarts

I/flutter (20143): getting image
D/Surface (20143): Surface::disconnect(this=0x9577d000,api=1)
D/GraphicBuffer(20143): unregister, handle(0xaa60e900) (w:720 h:1440 s:736 f:0x1 u:b00)
D/GraphicBuffer(20143): unregister, handle(0xaa610280) (w:720 h:1440 s:736 f:0x1 u:b00)
D/GraphicBuffer(20143): unregister, handle(0xaa610640) (w:720 h:1440 s:736 f:0x1 u:b00)
D/Surface (20143): Surface::disconnect(this=0x9577d000,api=-1)
D/Surface (20143): Surface::disconnect(this=0x9577c000,api=1)
D/GraphicBuffer(20143): unregister, handle(0x94f956c0) (w:720 h:1440 s:736 f:0x1 u:b00)
V/PhoneWindow(20143): DecorView setVisiblity: visibility = 4, Parent = ViewRoot{f21fea2 com.example.firstapp/com.example.firstapp.MainActivity,ident = 0}, this = DecorView@5af733[MainActivity]
Lost connection to device.
Exited (sigterm)

these is the simple code i am using , just change source with ImageSource.camera from image picker package

Future _getImage(BuildContext context, ImageSource source) async {
    print('getting image');
    File image = await ImagePicker.pickImage(source: source, maxWidth: 600);
    if(image == null){
      return null;
    }
    setState(() {
      print('file = image');
      _imageFile = image;
    });
    print('setting image');
    widget.setImage(image);
    Navigator.pop(context);
  }

Thanks in advance

Akshit Ostwal
  • 451
  • 3
  • 14
  • can you share output of `adb logcat` ? The log you pasted doesn't say much about the crash. Look for RuntimeException or NPE – Darshan Jul 28 '19 at 15:32
  • plz look at this... https://github.com/flutter/flutter/issues/48016#issuecomment-606538114 as its open issue on github, lots of developer has the same issue perticularlly with redmi devices. – Rajen Trivedi Apr 27 '20 at 13:35
  • Still, it happening in the latest version of image picker. Only the rear camera is the problem but it works well with front camera. – Vinoth Vino Jun 09 '20 at 04:14
  • Double-check Camera permissions first, pretty common issue. – Lalit Fauzdar Jun 10 '22 at 17:17

2 Answers2

0

Update, with the new Version of image_picker this issue has been solved.

Please follow official docs of https://pub.dev/packages/image_picker for more reference.

Akshit Ostwal
  • 451
  • 3
  • 14
-1

In runner Info.plist file, add these works for me

<string>Need to upload image</string>
<key>NSCameraUsageDescription</key>
<string>Need to upload image</string>
<key>NSMicrophoneUsageDescription</key>
<string>Need to upload image</string>
Ping Woo
  • 1,423
  • 15
  • 21