简体   繁体   English

Flutter 如何显示来自 base64 的图像

[英]Flutter how to display an image from base64

I am using an API that brings data like this:我正在使用 API 带来这样的数据:

"photos": [
    {
      "id": 6,
      "name": "Seguridad",
      "base64Image": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...",
    }
]

What I need to do is to display the photos in a screen in my App.我需要做的是在我的应用程序的屏幕上显示照片。 I've searched how to do it but I kept having the same error message: Exception: Invalid Image Data.我已经搜索了如何执行此操作,但我一直收到相同的错误消息:异常:图像数据无效。 I've read that what I need to do is to encode my base64 String, then decode that and then use it on Image.Memory, like this:我读到我需要做的是编码我的 base64 字符串,然后对其进行解码,然后在 Image.Memory 上使用它,如下所示:

String image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...';
var bs64 = base64.encode(utf8.encode(example));
Uint8List decodedImage = base64.decode(bs64);
// --------
Image.memory(decodedImage)

I've tried using different base64 strings but the error is always there.我试过使用不同的 base64 字符串,但错误始终存在。 I appreciate any help with this.感谢您对此提供的任何帮助。

Because you are not decoding base64 string.因为您没有解码base64字符串。

You have to remove data:image/jpeg;base64, (comma included)您必须删除data:image/jpeg;base64, (包括逗号)

you have to remove that because that data:image/jpeg;base64, just indicates that the string is image data with jpeg format which is encoded as base64 string.你必须删除它,因为data:image/jpeg;base64,只是表明该字符串是jpeg格式的图像数据,编码为base64字符串。

So, the actual base64 data starts after it and you have to decode that.因此,实际的base64数据在它之后开始,您必须对其进行解码。

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

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