首页 > 解决方案 > Flutter:如何将字节转换为文件以便将文件上传到 Firestore

问题描述

我正在尝试用颤动的文本为图像加水印,然后需要将该加水印的图像上传到 Firestore。当我尝试为字节添加水印并将字节存储到文件时,我收到了一个错误。

File: '����

请帮我解决这个问题。谢谢!

这是代码部分。

  _originalImage = ui.decodeImage(croppedFile.readAsBytesSync());
  ui.drawString(_originalImage, ui.arial_24, 100, 120, 'Hello World');
    // Store the watermarked image to a File
   List<int> resImage = ui.encodeJpg(_originalImage);
   print(resImage);
    setState((){
      _watermarkedImage = File.fromRawPath(Uint8List.fromList(wmImage));
      print(_watermarkedImage);
    });
  imageFile = _watermarkedImage;

当我在将字节存储到文件之前打印结果时,我得到了以下字节值。

255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 132, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 255, 192, 0, 17, 8, 2, 37, 2, 47, 3, 1, 17, 0, 2, 17, 1, 3, 17, 1, 255, 196, 1, 162, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 10

标签: flutterflutter-layout

解决方案


正如 Antonin 所建议的,您需要加载文件路径。使用它将您的字节存储在临时文件中,然后加载文件

final directory = await getTemporaryDirectory();
final filepath = "abc.png";
File imgFile = File(filepath);
imgFile.writeAsBytes(bytes); //your image bytes

现在使用该文件


推荐阅读