将位图转换为 Base64 字符串格式时如何解决内存不足错误,反之亦然?
我需要使用 SOAP 请求将一些捕获的图像上传到 .net Web 服务器。由于 SOAP 也接受图像文件的字符串,因此我使用 android.util.Base64 类将捕获的图像文件转换为字符串。
但是,当使用 Base64 编码格式将图像文件转换为字符串时,出现内存不足错误。
如何解决将图像文件转换为 Base64 格式字符串时出现内存不足错误?
I need to upload some captured images to a .net webserver using SOAP request. Since SOAP accepts strings for image files also, I am converting captured image files into string using android.util.Base64 class
.
But when converting image files into strings using Base64 encoding format, I am getting an out of memory error.
How can I resolve the out of memory error when converting image files into Base64 format strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 Ben 是在要求你切碎你的位图并分别对每个块进行编码。另一方面,您必须使用多个 Base64 块重建字节数组。每个块都应该排序,并且最好包含字节数组的最终大小,以便接收者知道要分配的大小。
类似于:
我不会严格依赖顺序将其写入 xml 流,以防您的接收者规范化您的 xml。
I think Ben is saying for you to chop up your bitmap and encode each chunk separately. On the other end, you'll have to reconstruct the byte array with the multiple base64 chunks. Each chunk should be ordered and probably best to include the final size of the byte array so that the receiver knows what size to allocate.
something like:
I wouldn't rely on the order to be strictly how it is written to the xml stream in case your receiver canonicalizes your xml.
您能否以块的形式读取每个图像文件,转换它们,将它们附加到文件中,然后在发送时以块的形式读回该文件?
Can you read each image file in chunks, convert those, appending them to a file as you go, and then read that file back out in chunks when sending?
解码为位图意味着更多的内存占用。特别是如果你只想从 JPEG 转换为 Base64,那就太浪费了。
在我看到的大多数答案中,他们将其转换为 JPEG >位图>字节[]> Base64。
大多数内存异常发生在将大 jpeg 解码为位图时。
我仍在寻找一个好的解决方案,但请查看这个答案。他正在将文件字节数组直接转换为 Base64。
https://stackoverflow.com/a/10160856/499752
Decoding to Bitmap would mean more memory foot print. Specially its a big waste if you just want to convert from JPEG to Base64.
In most of the answers I have seen they convert it as JPEG > BITMAP > Byte[] > Base64.
Most of the memory exception happens when you decode a big jpeg to bitmap.
I am still looking for a good solution but check this answer out. He is converting file byte array to Base64 directly.
https://stackoverflow.com/a/10160856/499752