在flutter中将应用程序/八位字节流转换为图像/jpeg/png?

Man*_*ath 4 ios dart firebase flutter firebase-storage

我试图使用Imagepicker库将图像上传到 Firebase 商店,

但是在控制台中,它显示的格式为 application/octet-stream,因为图像不可见。有没有什么可能的方法可以在上传时将该格式转换为 dart 中的图像。

Gün*_*uer 8

上传时将文件扩展名传递给文件名

FirebaseStorage.instance
        .ref()
        .child(path)
        .child('image.jpg');
Run Code Online (Sandbox Code Playgroud)

或传递元数据

FirebaseStorage.instance
        .ref()
        .child(path)
        .child('image');
imageStore.putFile(file, StorageMetadata(contentType: 'image/jpeg'));
Run Code Online (Sandbox Code Playgroud)

https://firebase.google.com/docs/storage/web/upload-files#add_file_metadata

添加文件元数据

上传文件时,您还可以为该文件指定元数据。此元数据包含典型的文件元数据属性,例如名称、大小和内容类型(通常称为 MIME 类型)。Cloud Storage 会自动从文件在磁盘上存储的文件扩展名推断内容类型,但如果您在元数据中指定 contentType,它将覆盖自动检测的类型。如果未指定 contentType 元数据且文件没有文件扩展名,则 Cloud Storage 默认为类型 application/octet-stream。有关文件元数据的更多信息,请参见使用文件元数据部分。

  • 你节省了我几个小时,谢谢! (2认同)