【问题标题】:How do I convert an image to base64 and base64 to image? The way I do it doesn't work如何将图像转换为 base64 和 base64 为图像?我这样做的方式行不通
【发布时间】:2019-06-11 11:35:02
【问题描述】:

这是一个示例代码。

var image = await ImagePicker.pickImage(source: ImageSource.camera);
var stringBytes = base64.encode(image.readAsBytesSync());
var bytes = base64.decode(stringBytes);
var newImage = new File.fromRawPath(bytes);
I/flutter (14608): The following FileSystemException was thrown resolving an image codec:
I/flutter (14608): Cannot open file, path = '����*�Exif
I/flutter (14608): 
I/flutter (14608): 
I/flutter (14608): Business
I/flutter (14608): 11:42:34
I/flutter (14608): �
I/flutter (14608):  
I/flutter (14608): ��
I/flutter (14608): ��
I/flutter (14608): %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������
I/flutter (14608): ��
I/flutter (14608): $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������
I/flutter (14608): ��ت���U-%�����^
I/flutter (14608): }�m���
I/flutter (14608): u���V�N6R���
I/flutter (14608): j_8}W,1�ڹ�?ܻw^��� ��6��ꗚm���E[ϓ�������>���X�W��y������=�[!��2!ډ�'8�Mk^ܾ��eS�

未知字符列表继续。

我做错了什么?

我想把它转换成base64,因为我要把它添加到数据库中。

【问题讨论】:

标签: dart flutter decode encode


【解决方案1】:

不,您正在传递图像的内容以创建一个以内容作为原始路径的新文件,这是行不通的。

new File.fromRawPath 确实如此,根据docs

从原始路径创建一个 File 对象,即一个字节序列 由操作系统表示。

您要做的是创建一个文件并将内容存储到该文件中,可以这样完成(Source):

var imageFile = File('myimage.jpg');
var sink = imageFile.openWrite();
sink.write(bytes);
await sink.flush();
await sink.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多