【问题标题】:Upload Image Flutter Storage上传图片 Flutter 存储
【发布时间】:2020-02-29 10:27:46
【问题描述】:

我在我的应用程序中使用“image_picker_web/image_picker_web.dart”。现在,当我选择图像时,显示此图像,它的工作。但是当我尝试将图像保存在存储中时,什么也没有发生。

Uint8List _image1;

Future getImg() async {
Uint8List tempImg = await ImagePickerWeb.getImage(asUint8List: true);

if (tempImg != null) {
  setState(() {
    // debugPrint(tempImg.toString());
    _image1 = tempImg;
  });
}}

保存在存储中:

void uploadImg() async {
final StorageReference productImg =
    FirebaseStorage.instance.ref().child('ProductImg');
var timekey = DateTime.now();
final StorageUploadTask uploadTask =
    productImg.child(timekey.toString() + 'jpg').putData(_image1);
var imageUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
url = imageUrl.toString();
print('Image Url' + url);}

按钮:

RaisedButton(
   onPressed: () {
     uploadImg();
 }

我做错了什么?

【问题讨论】:

  • url 在控制台上打印什么?
  • 什么也不打印。但是调试打印打印 [255, 216, 255, 224, 0, 16, 74,....]

标签: flutter


【解决方案1】:

试试这个:

final file = File.fromRawPath(uint8list);
final ref = FirebaseStorage.instance.ref().child("users/some_id/ProductImg.jpg");
final task = ref.putFile(file, StorageMetadata(contentType: "image/png"));
final snapshot = await task.onComplete;
if (snapshot.error != null) throw snapshot.error;
final downloadURL = await snapshot.ref.getDownloadURL();

【讨论】:

  • 对不起,它不起作用。仍然没有打印,也没有任何反应。希望你有办法。
  • 不把uint8list转换成File,能不能直接捕获File,然后执行剩下的代码?我做到了,它奏效了。
  • 我是flutter的新手,如果不打扰你,你能解释一下怎么做吗?
  • 谁能指导我??
  • @AndreyKhan 我以前没试过那个插件,所以我帮不了你太多。如果您想从手机上传照片,我可以提供完整的解决方案。
猜你喜欢
  • 2021-05-08
  • 2020-10-05
  • 2021-06-28
  • 2011-02-08
  • 2020-12-30
  • 2018-10-24
  • 2019-01-22
  • 2021-12-23
相关资源
最近更新 更多