How can i covert Image file to binary data ? I'm using a library call image_picker for pick the image from gallery or camera. And I want to convert the image that I picked to binary data.
File image = await ImagePicker.pickImage(source: ImageSource.gallery)
(image as Image).toByteData // the method toByteData here is not pop up.
toByteData() method allows converting the image into a byte array. We need to pass the format in the format argument which specifies the format in which the bytes will be returned. It'll return the future that completes with binary data or error.
final pngByteData = await image.toByteData(format: ImageByteFormat.png);
ImageByteFormat enum contains the following constants.
For more information about ImageByteFormat, please have a look at this documentation.
Update : If you want to convert the image file into bytes. Then use readAsByte() method.
var bytes = await ImagePicker.pickImage(source: ImageSource.gallery).readAsBytes();
For converting image into a file, Check out this answer.
simply use this method
var bytes = await File('filename').readAsBytes();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With