Flutter: Upload user image

Asked

Viewed 508 times

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'),
        ),
      );
    }
  }

1 answer

0


To solve your problems you can use the library path. Just remember to import at the beginning of your file:

import 'package:path/path.dart';

receive the image format type, example ("image/png" or "image/jpg").

There is the function Extension for that reason:

String extensao = extension(file.path); // exemplo de resultado: ".jpg"

It is worth making clear that this extension will include the point. If you want to remove, just remove the first character:

print(extensao.substring(1)) // exemplo de resultado "jpg"

And I need to receive: image_picker540161743069966467.jpg

For this you can use its function basename:

String NomeDoArquivo = basename(file.path); // exemplo de resultado: "imagem.jpg"
  • 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 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. Try readAsBytesSync(File(file.path)) or something like.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.