【问题标题】:Flutter How can we convert Uint8List image to .jpg / .png?Flutter 我们如何将 Uint8List 图像转换为 .jpg / .png?
【发布时间】:2023-03-10 08:58:01
【问题描述】:

我正在使用flutter_inappwebview 截取网页截图。屏幕截图为Uint8List 格式。我想将其更改为File,以便可以将其保存在我的服务器中。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    这是一个示例,path 是您的图像的完整路径,img 是您的 Uint8List

    File newFile = await File(path).writeAsBytes(img);
    

    【讨论】:

    • 谢谢。有效。我在下面写了完整的解决方案。没有你的帮助,这是不可能的。再次感谢。
    【解决方案2】:
    getApplicationDocumentsDirectory() is from path_provider package.
    
    onPressed: () async {
              final result = await webView.takeScreenshot();
              final directory = (await getApplicationDocumentsDirectory()).path; // to get path of the file
              String fileName = DateTime.now().toIso8601String(); // the name needs to be unique every time you take a screenshot
              var path = '$directory/$fileName.png';
    
              File image = await File(path).writeAsBytes(result); // thanks to the answer above by @Guillaume Roux
    
               showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    content: Image.file(image),
                  );
                },
              );
            },
    

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 1970-01-01
      • 2012-06-01
      • 2013-01-08
      • 1970-01-01
      • 2013-05-10
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多