4

I'm thinking about using the image picker package for my app. The thing is that I only want the user to be allowed to select images that are of type JPEG, JPG, or PNG. Is there a way to go about doing this?

Thank You.

Patttt
  • 51
  • 1
  • 3
  • This specific `image_picker` already has such an option. It uses `XTypeGroup`, and you can create a list of extensions you want to read based on it. Check it in their repo -> https://github.com/flutter/plugins/blob/7686be7ecbe1cc049423715b5e2d897cb63a43c1/packages/file_selector/file_selector_web/test/utils_test.dart#L13 Underneath, this does the very same thing that @Roman Jaquez has already pointed out. – mutantkeyboard Feb 24 '22 at 22:00

2 Answers2

6

The file_picker package gives you this option already:


FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowMultiple: false,
      allowedExtensions: ['jpg', 'jpeg', 'png'],
    );
Roman Jaquez
  • 2,499
  • 1
  • 14
  • 7
  • 2
    It seems that this functionality exists for file_picker, and not image_picker. I was hoping to use image_picker solely since both taking a picture and selecting a picture is what I need. – Patttt Feb 25 '22 at 15:02
1

If I understand correctly you would want to show a message if the user selects an image with an extension other than the one you want? If so you could just grab whatever the path of the image is.

Run some if else statements and check if the file extension is any of the ones you want. If you need some help getting the path of the images from image_picker let me know.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
domtom
  • 27
  • 5