android - 如何将图像转换为base64以使用http帖子请求发送

标签 android ios flutter dart

我正在尝试使用Requests包将Base64字符串发送到服务器。我正在使用Flutter image Picker包获取图像文件,这是我的代码片段:
获取图像

  File _image;
  final picker = ImagePicker();

  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      _image = File(pickedFile.path);
    });
  }
转换并发送
    List<int> imageBytes = _image.readAsBytesSync();
    String  base64Image = base64Encode(imageBytes);

      var r = await Requests.post(
        'my_URL',
        headers: {'content-type': 'multipart/form-data'},
        body: {
          'image':'$base64Image',
        }
      );

这会将null发送到服务器,有关如何正确执行操作的任何建议?

最佳答案

更改此行

List<int> imageBytes = _image.readAsBytesSync();
List<int> imageBytes = await _image.readAsBytesSync();
这可能会有所帮助。

关于android - 如何将图像转换为base64以使用http帖子请求发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62739755/

相关文章:

android - Google Nexus 7 无法通过 USB 识别

Android 场景和过渡、动画之间的区别以及何时使用它们

ios - 错误 : Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'

animation - 如何检查AnimatedContainer是否完全动画?

flutter - 在 DART 中 - toList() 方法不会将 int 的 Iterable 转换为列表,但是当它应用于 print() 内部时,它可以正常工作。为什么?

android - 如何从自定义操作栏的顶部和底部删除边距

android - 网页左侧在 Android 手机上被截断,但在 Chrome、Safari 和 Firefox 上看起来不错

ios - 如何以编程方式从 iOS 访问 Safari 的阅读列表

ios - 进入viewController后如何加载数据?

class - Dart 确保类中的可选列表参数不为空