【问题标题】:Flutter: How to Convert bytes to a File in order to upload the file to FirestoreFlutter:如何将字节转换为文件以便将文件上传到 Firestore
【发布时间】:2021-02-28 07:52:41
【问题描述】:

我正在尝试用颤动的文本为图像加水印,然后需要将该加水印的图像上传到 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

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

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

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

    现在使用文件

    【讨论】:

    • 我没有文件路径。我所拥有的只是字节(请检查我上面的问题)。现在我需要将字节转换为文件。请帮我将字节转换为文件。这样我就可以将文件上传到 Firestore。
    • 我已经给出了创建文件的例子。文件路径可以设置为任何值。那就是将创建文件的名称
    • 好的,将尝试实现代码!这个“最终目录 = await getTemporaryDirectory();”有什么用?我也收到此错误 - “未处理的异常:FileSystemException:无法打开文件,路径 = 'abc.png'(操作系统错误:只读文件系统,errno = 30)”
    • 是的..正如埃里克所说,请使用路径提供插件
    【解决方案2】:

    这就是我在 Ganesh Bhat 的帮助下解决问题的方法。

      _originalImage = ui.decodeImage(imageFile.readAsBytesSync());
      ui.drawString(_originalImage, ui.arial_48, 250, 340, 'Hello World');
        // Store the watermarked image to a File
       wmImage = ui.encodeJpg(_originalImage);
       Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
       String appDocumentsPath = appDocumentsDirectory.path; 
       filePath = '$appDocumentsPath/abc.jpg';
       File imgFile = File(filePath);
       imgFile.writeAsBytes(wmImage);
       print(imgFile);
       imageFile = imgFile;
    

    【讨论】:

      【解决方案3】:

      你试图从一个路径中获取一个文件,但你给它字节,你需要加载正确的路径或将字节转换为文件

      【讨论】:

      • 那么如何将字节转换为文件
      【解决方案4】:

      在您的代码中某处您有一个未初始化的对象 (null),在该对象上执行 path 方法会导致错误。通过调试或将它们记录到控制台来检查您的所有 文件系统 对象是否已正确初始化。

      尝试检查 wmImageUint8List.fromList(wmImage) 它们指向的内容 - 我的猜测是这些实体之一已分配 null

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-20
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多