Set Size Limit for expo ImagePicker

How to set a max size to pick a mage in ImagePicker.launchImageLibraryAsync? Or after pic an image how do I calculate the picked file size, so that I can give an error message to the user that he selecting more than 1 Mb or whatever size limit we have.
Thank You.

Hey @atha.cyanberg,

This isn’t possible with ImagePicker currently but it’s something we’re going to look into.

Cheers,
Adam

Thanks for your reply.

1 Like

Thanks for bringing this to our attention!

If the reason for the limit is to prevent too large of images being uploaded to an API, you could look at resizing the image to something smaller after you get it from the photo library via ImageManipulator (https://docs.expo.io/versions/latest/sdk/imagemanipulator/#__next). Phones don’t do a great job of exposing how large an image is anyway, so it’ll be luck of the draw as to whether or not they choose an image within the limits, so just let them choose any image and have your app shrink it so it’s not too big. Phone camera images are huge now and you can get away with significantly reducing the size for most use cases (other than for storing original photos).

Hi, Thanks for your answer, this is really helpful but one problem still remains, how to resize the image at the time of pick from my mobile file system? Anyway, I need to show the error message to the user that he selected a large image. How can I do that?

You can take the result from ImagePicker.launchImageLibraryAsync and feed the uri prop on that object to ImageManipulator.manipulateAsync. Image resizing works if it comes from the camera roll or the camera (these actually both come out as file refs).

If you set a dimensions limit on the image by resizing it, I believe showing an error if the image is too large will be unnecessary. An image constrained to a 1000x1000 box can only get so large. If 1 MB is a hard limit set by your server, setup properties on the ImageManipulator that will basically guarantee your image is under 1 MB (besides resolution, you can also set JPEG quality to affect size). Keep in mind that your user may not have a good way to reduce the file size of the image from their device (I don’t even know how I would do this on my phone, and I’m a phone app developer), so, if they get this error, they may be stuck being unable to upload a photo they need to upload.

If you do still need to check file sizes, there is the option of ejecting and using a library like react-native-fs, which has more file reading/ writing features.