【问题标题】:Convert Uint8List to File将 Uint8List 转换为文件
【发布时间】:2020-06-15 19:50:08
【问题描述】:

我正在使用运行良好的图像选择器网络。我可以在Image.memory() 中显示图像,但此图像的格式为 Uintlist8。为了保存在存储需要格式File,我的问题是如何在 Firebase 存储 中保存图像。

网络图片选择器:

class _SecondPageState extends State<SecondPage> {
  final _formkey = GlobalKey<FormState>();
  Uint8List _image;

  getImage() async {
    Uint8List tempImg = await ImagePickerWeb.getImage(asUint8List: true);
    if (tempImg != null) {
      setState(() {
      _image = tempImg;
    });
  }
}

【问题讨论】:

    标签: flutter dart flutter-web


    【解决方案1】:

    请尝试....

    final _formkey = GlobalKey<FormState>();
      Uint8List _image;
     getImage() async {
     Uint8List tempImg = await ImagePickerWeb.getImage(asUint8List: true);
     if (tempImg != null) {
      setState(() {
      _image = tempImg;
      final tempDir = await getTemporaryDirectory();
      final file = await new File('${tempDir.path}/image.jpg').create();
      file.writeAsBytesSync(_image);
      });
     }
    }
    

    【讨论】:

    • 现在不在Image,memory()中显示图片
    • 你可以试试 Image.file(file);显示图像。
    • 我也试过了,文件在 setState 所以我创建了File img 并做了这个文件= img。之后在 Image.file(img) 中。它不显示。希望你有办法
    • 就像一个魅力!需要从 Uin8List 图像中获取路径字符串,这是我找到的唯一解决方案。
    • 这能回答问题吗? path_provider 不适用于网络。你得到MissingPluginException
    猜你喜欢
    • 2021-07-26
    • 2022-08-18
    • 2020-05-20
    • 2022-10-05
    • 2021-09-19
    • 2022-01-18
    • 2020-08-25
    • 2021-04-05
    • 2020-07-25
    相关资源
    最近更新 更多