JulioStyle88
JulioStyle88

Reputation: 105

Serialize image to string

Good friends.

A couple of months I am looking for a way to serialize an image I have stored in a imageView.

This image was taken by the camera and start the ImageView, but the question esque need serialize and pass it to a text string.

I have searched high and low on google and forums, but none have found an answer.

Anyone know some method of how I can do?.

Upvotes: 0

Views: 1243

Answers (1)

Malcolm Smith
Malcolm Smith

Reputation: 3580

If you want to move binary data around safely as text, you could Base64 encode the binary data. If you are using an Android API that doesn't include the Base64 utils, see: How to use Base64 (included in android since api 8 (2.2)) in a android project api 3 (android 1.5)?

Once your binary data is Base64 encoded it can be safely passed through systems that are only expecting 'plain' ascii text.

Bear in mind that your bitmap data will be uncompressed and will therefore generate a large amount of text when Base64 encoded (Base64 encoding results in the bytes required to represent the data increasing by 1/3). To mitigate this you should compress the Bitmap image data before Base64 encoding it, using something like:

try
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
    bitmap.compress(CompressFormat.JPEG, COMPRESSION_AMOUNT, baos);     
    byte[] compressedBytes = baos.toByteArray();
}

Where you want to set the COMPRESSION_AMOUNT to a value between 50-80 depending on the trade off between size and quality you are wanting.

Any clients recieving your data will first need to Base64 unencode, and then parse the resulting bytes as JPEG data to recreate the image.

Upvotes: 3

Related Questions