首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ImagePicker上传图像

用ImagePicker上传图像
EN

Stack Overflow用户
提问于 2020-11-05 13:29:48
回答 1查看 83关注 0票数 0

我正在做一个颤振项目,我试着学习一些关于图像选择器的教程,但是它不起作用。我不知道为什么。我的图像选择器工作,我可以选择一个图像,但我的帖子请求无效。有人看到什么不对劲了吗?谢谢你的阅读

代码语言:javascript
复制
class _HomeWidgetState extends State<HomeWidget> {
  File imageFile;
  final picker = ImagePicker();

_gallery() async {
    final pickedFile = await ImagePicker.pickImage(source: ImageSource.gallery);

  this.setState(() {
    if (pickedFile != null) {
      imageFile = File(pickedFile.path);
    }
    else {
      print('no image');
    }
    });

  String name = 'Demo Title';
  String desc = 'Ma super description :)';
  List<int> imageBytes = imageFile.readAsBytesSync();
  String image = base64Encode(imageBytes);

  String url = 'https://api.imgur.com/3/upload';
  Map<String, String> headers = {"Authorization": "Bearer " + globals.access_token};
  String json = '{"title": "$name", "name": "$name", "description": "$desc", "image": "$image", "type": "base64"}';

  final response = await http.post(url, headers: headers, body: json);
  if (response.statusCode != 200) {
    return null;
  }
  print("********end************");
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-05 15:59:22

你能试试这个吗?

代码语言:javascript
复制
class _HomeWidgetState extends State<HomeWidget> {
  File imageFile;
  final picker = ImagePicker();

_gallery() async {
    final pickedFile = await ImagePicker.pickImage(source: ImageSource.gallery);

  this.setState(() {
    if (pickedFile != null) {
      imageFile = File(pickedFile.path);
    }
    else {
      print('no image');
    }
    });

  String name = 'Demo Title';
  String desc = 'Ma super description :)';
  List<int> imageBytes = imageFile.readAsBytesSync();
  String image = base64Encode(imageBytes);

  String url = 'https://api.imgur.com/3/upload';
  Map<String, String> headers = {"Authorization": "Bearer " + globals.access_token};
  Map<String, dynamic> json = {"title": name, "name": name, "description": desc, "image": image, "type": "base64"};

  final response = await http.post(url, headers: headers, body: json);
  if (response.statusCode != 200) {
    return null;
  }
  print("********end************");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64698250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档