简体   繁体   English

如何在 flutter 中将 Blob 转换为图像

[英]How to Transform a Blob to An Image in flutter

i want to display an image stored in mysql database the problem is that i can't convert the blob format into a Uint8list;我想显示存储在 mysql 数据库中的图像问题是我无法将 blob 格式转换为 Uint8list; i searched and found soulitions but none of them works Grab the blob from JSON:我搜索并找到了灵魂,但它们都不起作用 从 JSON 中获取 blob:

var blob = yourJSONMapHere['yourJSONKeyHere']; var blob = yourJSONMapHere['yourJSONKeyHere'];

var image = BASE64.decode(blob); var image = BASE64.decode(blob); // image is a Uint8List // 图像是一个 Uint8List

Now, use image in a Image.memory现在,在 Image.memory 中使用图像

new Container( child: new Image.memory(image));新容器(孩子:新的Image.memory(图像));

this soulition didn't work because base64.decode need a string source not a blob file to convert这个灵魂没有工作,因为 base64.decode 需要一个字符串源而不是一个 blob 文件来转换

Don't know if it is relatable to this particular case but had similar issue with Cloud Firestore.不知道它是否与这种特殊情况有关,但与 Cloud Firestore 有类似的问题。

Created blob for storing in this way:以这种方式创建用于存储的 blob:

Blob myBlob = Blob(await audioFile.readAsBytes());

saved to Firestore in one field as usual Then tried to read it back and couldn't figure it out how to get Uint8List from blob I get back from Firestore.像往常一样在一个字段中保存到 Firestore 然后尝试将其读回,但无法弄清楚如何从我从 Firestore 回来的 blob 中获取 Uint8List。

my solution:我的解决方案:

//extract blob from field of Firestore document
Blob audioBlob = audioDocFromDb.get("fieldName");

//use .bytes on blob from this source
//package:cloud_firestore_platform_interface/src/blob.dart
Uint8List audioBytes = audioBlob.bytes;

this worked for me.这对我有用。 In my case I was packing up recorded audio and trying to play it back.就我而言,我正在打包录制的音频并尝试播放它。

I had this problem too, i know the solution now, after many attempts: Dont forget to upvote!我也遇到了这个问题,经过多次尝试,我现在知道了解决方案:不要忘记投票!

Uint8List image = Uint8List.fromList(blob.toBytes()); Uint8List 图像 = Uint8List.fromList(blob.toBytes());

Image.memory(image);图像.内存(图像);

This function has always saved me for getting bytes from a file that is uploaded to a URL.这个 function 总是帮我从上传到 URL 的文件中获取字节。

import 'package:http/http.dart' as http;导入“包:http/http.dart”为 http;

// urlImageBlob is the URL where our file is hosted. // urlImageBlob 是我们文件所在的 URL。

Uint8List fileBytes = await http.readBytes(Uri.parse(urlImageBlob)); Uint8List fileBytes = 等待 http.readBytes(Uri.parse(urlImageBlob));

// Display if are image. // 如果是图像则显示。 Image.memory(fileBytes); Image.memory(fileBytes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM