Skip to content Skip to sidebar Skip to footer

Flutter Image Picker Crash

I want to ask if you ever encounter the app being crash right after selecting an image from the gallery/camera using image_picker plugin version 0.6.3+4 on Android? Already searchi

Solution 1:

found out the problem, with the latest version of Flutter on stable channel 1.12.13+hotfix.9, you can't use plugin image_picker and image_picker_saver together, still don't know why, but you can add a comment to this answer if you know about it, thanks.

Solution 2:

Image_picker_saver will not work with image_picker you can use any other plug in similar to image_picker_saver like https://pub.dev/packages/image_gallery_saver

Solution 3:

https://pub.dev/packages/image_picker is worst package it cant handle android way camera correctly .... please see flutter camera example and you will land safely with out any issues... refer this link and copy past last code sinnpet given in that cookbook

Solution 4:

The problem with using the plugin permission_handler is iOS will complain about .plist info. Until you are certain about multiple permission like camera, calendar, contacts etc... I would not prefer to use this.

This is how i did -

checkAndRequestCameraPermissions() async {
    final picker =ImagePicker();
    try {
      final image =await picker.getImage(
        source: ImageSource.camera,
        imageQuality: 75,
        maxHeight: 300,
        maxWidth: 400,
      );
    } catch (e) {
      print(e.code);
    }
  }

you can check for exception code in your catch bloc like e.code == 'camera_access_denied' then you can prompt user about enabling it from device Settings, on Android it ask anyway all time if it is not granted/disabled.

Solution 5:

image_picker plugin does not support permissions condition anymore. For instance, when we try to access camera from an app, the app asks us for permission. Now, since image_picker plugin has removed that condition, we need to ask the permission manually using permission_handler plugin. Read more details about this change here

Use this plugin to add the request manually i.e on runtime and you should be able to get rid of this crash.

Sample Code:

// get permissions
  Future<PermissionStatus> _getPermission(perm) async {
    PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(perm);
    if (permission != PermissionStatus.granted &&
        permission != PermissionStatus.disabled) {
      Map<PermissionGroup, PermissionStatus> permisionStatus =
      await PermissionHandler()
          .requestPermissions([perm]);
      return permisionStatus[perm] ??
          PermissionStatus.unknown;
    } else {
      print(permission);
      return permission;
    }
  }

  Future selectGallery() async {
    // check permission
    PermissionStatus permissionStatus = await _getPermission(PermissionGroup.photos);
    if (permissionStatus == PermissionStatus.granted) {
      var image = await ImagePicker.pickImage(source: ImageSource.gallery);
      setState(() {
        accessDenied = false;
        // do your work
      });
    } else {
      accessDenied = true;
      setState(() {});
      throw PlatformException(
        code: 'PERMISSION_DENIED',
        message: 'Access to location data denied',
        details: null,
      );
    }    
  }

  Future selectCamera() async {
    // check permission
    PermissionStatus permissionStatus = await _getPermission(PermissionGroup.camera);
    print(permissionStatus);
    if (permissionStatus == PermissionStatus.granted) {
      var image = await ImagePicker.pickImage(source: ImageSource.camera);
      setState(() {
        accessDenied = false;
        // do your work
      });
    } else {
      accessDenied = true;
      setState(() {});
      throw PlatformException(
        code: 'PERMISSION_DENIED',
        message: 'Access to location data denied',
        details: null,
      );
    }
  }

Post a Comment for "Flutter Image Picker Crash"