Home > database >  Flutter image_picker problem on iOS device Flutter
Flutter image_picker problem on iOS device Flutter

Time:09-16

I'm running into a very odd error on M1 Macbook when developing for iOS devices. This is a simple picture picking functionality within the Flutter template (full code here). This works perfectly fine on Android, but not Apple iPhone simulator (running iOS 14 on simulator), where it gives an error that returns null.

      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await _getImageFromGallery();
          //print('Image selected');
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),

Below is the image picking function for image_picker: ^0.8.3 1. I tried a few different versions with minimal success.

  final picker = ImagePicker();
  File? imagePath;

  Future _getImageFromGallery() async {
    try {
      var pickedFile = await picker.pickImage(source: ImageSource.gallery);
      print('pickedFile: ${pickedFile!.path}');
      setState(() {
        imagePath = File(pickedFile.path);
      });
    } catch (error) {
      print('error: $error');
    }
  }

If anyone could help that would be much appreciated!

CodePudding user response:

Might be a permission problem, you can check out the permission_handler package and see if that fixes your issue.

CodePudding user response:

Add This Permission in Your Android mainfest File

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

For IOS Add This Line to /ios/Runner/Info.plist:

<key>NSMicrophoneUsageDescription</key>
<string>Microphone Access Required</string>
<key>NSMotionUsageDescription</key>
<string>Motion Usage Required</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Please allow access to photo library so you can easily upload your photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Please allow access to photo library so you can easily upload your photos.</string>

Add this line in pubspec

image_picker : ^0.8.4

This is code i am currently using for my app.

onTap: () async{
    String path = await getImagePath();

    print(path);
    if(path != "Exceed Limit" && path!="noselect")
    {
      File imagefile = File(path);
    }
}

Future<String> getImagePath() async{
     final ImagePicker _picker = ImagePicker();
    // Pick an image
    final XFile? image = await _picker.pickImage(
        source: ImageSource.gallery);
    if (image?.path != null) {
        print(image!.path);
        int size = await image.length();
        double sizevalueinmb = size / (1024 * 1024);
        if (sizevalueinmb < 5) {
            String path = image.path;
            return path;
        } 
        else {
            print("Your Image File Exceeded Size Limit of 5mb !");
            return "Exceed Limit";
        }
    } 
    else {
    print("User Not Selected Any Image");
    return "noselect";
    }
}
  • Related