Flutter:将图像上传到Firebase存储

时间:2018-05-14 19:18:50

标签: firebase dart image-uploading flutter firebase-storage

我有一张从画布PictureRecorder获得的图像。现在我想将其上传到Firebase存储。我的问题是将其转换为png文件进行上传。我对转换图像了解不多,所以不确定如何以上传为png文件的方式对其进行操作。

final picture = recorder.endRecording();
final img = picture.toImage(640, 360);
final pngBytes = await img.toByteData();

final Directory systemTempDir = Directory.systemTemp;
final File file = await new File('${systemTempDir.path}/foo.png').create();
file.write?????(pngBytes);     <-- Not sure how to write the file here
final StorageReference ref =
      storage.ref().child('images').child('image.png');
final StorageUploadTask uploadTask =
      ref.putFile(file);

2 个答案:

答案 0 :(得分:1)

知道了!

final picture = recorder.endRecording();
final img = picture.toImage(640, 360);
final pngBytes = await img.toByteData(format: ImageByteFormat.png);
Uint8List finalImage = Uint8List.view(pngBytes.buffer);

final Directory systemTempDir = Directory.systemTemp;
final File file = await new File('${systemTempDir.path}/foo.png').create();
file.writeAsBytes(finalImage);
final StorageReference ref = storage.ref().child('images').child('image.png');
final StorageUploadTask uploadTask = ref.putFile(file);

答案 1 :(得分:0)

您可以使用imagemagik进行文件转换。 This link为您提供了关于如何做到这一点的合理详细信息,因此我不会在这里挖掘。或者,您could write a cloud function在服务器上执行转换。取决于您的用例。

HTH。