I'm new to flutter and in an app I'm building I use ImagePicker plugin. with that the file path image is viewed in my app after I get it from camera/gallery. the image.path is like this
/storage/emulated/0/Android/data/.../files/Pictures/234d9437-8652-48de-a2b6-711b5f8b702d3492716976084944343.jpg
I need to get the image "234d9437-8652-48de-a2b6-711b5f8b702d3492716976084944343.jpg" part from that and send it to a backend db. I need to convert this before sending. Backend only accept Images in FileFormat.
How can get the image from the file path. in this case it's the variable _imageURI. Then from that retrieve the image and convert it into a FileFormat. After that I need to pass it to a Backend using a json POST request.
In my json request I have a field for 'image': that I need to set the value got from image selected in file format and set to there. how can do this? can someone explain me with a code ? much appreciated.
My Code
File _imageURI;
Future _getImageFromCamera() async {
var petImage = await ImagePicker.pickImage(source: ImageSource.camera); //or gallery
setState(() {
_imageURI = petImage;
print(_imageURI.path);
}
}
Image Viewed as
Container(
width: 120.0,
height: 120.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.cover,
image: new FileImage(
_imageURI,
scale: 1.0,
),
),
),
),
Json Request
dogData = {
{
"user_email": "[email protected],
"user_token": "thisistoken",
"pet": {
"age": "integer",
"birth_date": "%d %b %Y (01 JAN 1996)",
"image": "!Swagger doesn't allow to put file upload. Use formdata instead of base64 in frontend module.",
"name": "string",
"sex": "string",
"user_id": "id"
}
}
My API call
final pet = await CallApi().createThePet(dogData, 'pets/create');
////
Future<dynamic> createThePet(data, apiUrl) async{
var fullUrl = _baseUrl + apiUrl; // + await _getToken();
final response = await http.post(fullUrl, body: jsonEncode(data), headers: _setHeaders());
....
Hope this helps,it is working fine with my project Install imagepicker,mime,http and import it
Initialize variable
View for showing image after selecting
ImagePicker from camera
Submit Function
You can checkout my github for image upload with either gallery or cameragithub