将位图转换为 Base64 字符串格式时如何解决内存不足错误,反之亦然?

发布于 2024-11-04 04:56:12 字数 200 浏览 0 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-11-11 04:56:12

我认为 Ben 是在要求你切碎你的位图并分别对每个块进行编码。另一方面,您必须使用多个 Base64 块重建字节数组。每个块都应该排序,并且最好包含字节数组的最终大小,以便接收者知道要分配的大小。

类似于:

byte[] bitmap = byte[size];
int j = 1;
write to xml stream: final size = size
for (i=0; i<size; i+chunkSize) {
    write to xml stream: base64.encode(bitmap from i to i+chunkSize)
    write to xml stream: order = j++
}

我不会严格依赖顺序将其写入 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:

byte[] bitmap = byte[size];
int j = 1;
write to xml stream: final size = size
for (i=0; i<size; i+chunkSize) {
    write to xml stream: base64.encode(bitmap from i to i+chunkSize)
    write to xml stream: order = j++
}

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.

青芜 2024-11-11 04:56:12

您能否以块的形式读取每个图像文件,转换它们,将它们附加到文件中,然后在发送时以块的形式读回该文件?

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?

涫野音 2024-11-11 04:56:12

解码为位图意味着更多的内存占用。特别是如果你只想从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文