sathish
sathish

Reputation: 285

How to resolve out of memory error when converting bitmap to Base64 string format and vice versa?

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?

Upvotes: 1

Views: 3620

Answers (3)

Siddharth Menon
Siddharth Menon

Reputation: 587

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

Upvotes: 0

David W.
David W.

Reputation: 41

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.

Upvotes: 4

Ben Williams
Ben Williams

Reputation: 6167

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?

Upvotes: 0

Related Questions