0
Hello, I am trying to send via API the image of the user at the time of registration. I need to receive the image in Base64 and receive the image format type, example ("image/png" or "image/jpg").
In my structure I can select the image and store it in cache, but I’m not able to capture the image type (png or jpg).
Also I’m getting the image this way:
File: '/data/user/0/com.example.Myapp/cache/image_picker540161743069966467.jpg'
And I need to receive:
image_picker540161743069966467.jpg
My structure capture the selected user image:
I store it later in a variable to send via POST:
List<int> retrato = userManager.retrato.readAsBytesSync();
'retrato': base64Encode(retrato),
'typeImage': '',
ImageSourceSheet({this.onImageSelected});
final Function(File) onImageSelected;
final ImagePicker picker = ImagePicker();
@override
Widget build(BuildContext context) {
if (Platform.isAndroid) {
return BottomSheet(
onClosing: () {},
builder: (_) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
FlatButton(
onPressed: () async {
final PickedFile file =
await picker.getImage(source: ImageSource.camera);
onImageSelected(File(file.path));
},
child: const Text('Câmera'),
),
FlatButton(
onPressed: () async {
final PickedFile file =
await picker.getImage(source: ImageSource.gallery);
onImageSelected(File(file.path));
},
child: const Text('Galeria'),
),
],
);
});
} else {
return CupertinoActionSheet(
title: const Text('Selecionar imagem'),
message: const Text('Escolha a origem da imagem'),
actions: <Widget>[
CupertinoActionSheetAction(
onPressed: () async {
final PickedFile file =
await picker.getImage(source: ImageSource.camera);
onImageSelected(File(file.path));
},
child: const Text('Câmera'),
),
CupertinoActionSheetAction(
onPressed: () async {
final PickedFile file =
await picker.getImage(source: ImageSource.gallery);
onImageSelected(File(file.path));
},
child: const Text('Galeria'),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Cancelar'),
),
);
}
}
Thank you very much, I’m already getting access to what I need. But I have the error "The method 'readAsBytesSync' isn’t defined for the type 'String'." when trying to convert the basename(file.path) string into bytes to encoding in Base64.
– JTeles
@Jteles I suggest asking another question regarding this, but what you should "find" in base 64 is not the name of the file, but the file itself (its contents). The function
readAsBytesSync
is a "File" function and not a String. TryreadAsBytesSync(File(file.path))
or something like.– Naslausky