65

I want to encode and decode Bitmap object in string base64. I use the Android API10,

I have tried, with no success, to use a method in this form to encode a Bitmap.

public static String encodeTobase64(Bitmap image) {
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.e("LOOK", imageEncoded);
    return imageEncoded;
}
Christoph
  • 47,569
  • 8
  • 87
  • 187
AndreaF
  • 11,975
  • 27
  • 102
  • 168

3 Answers3

213
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

public static Bitmap decodeBase64(String input)
{
    byte[] decodedBytes = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

Example usage:

String myBase64Image = encodeToBase64(myBitmap, Bitmap.CompressFormat.JPEG, 100);
Bitmap myBitmapAgain = decodeBase64(myBase64Image);
Roman Truba
  • 4,401
  • 3
  • 35
  • 60
  • but the length is soooo long that the server is returning `Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property`, is there a way to overcome it ?? @Roman Truba – Logic Feb 24 '16 at 08:36
  • 1
    @Logic base64 strings for images must be passed as POST request body data. I don't think, large images can be fit in JSON. – Roman Truba Mar 10 '16 at 11:05
  • @RomanTruba when i convert the class containing this encodeToBase64 string to json string using gson i got java.lang.OutOfMemoryError: Failed to allocate a 128887990 byte allocation with 16777216 free bytes and 70MB until OOM.How can I convert such strings to json string?? – KJEjava48 Apr 18 '17 at 13:09
  • @KJEjava48 use smaller images – Roman Truba Apr 18 '17 at 15:06
  • @RomanTruba When i upload images compreesiing it and then converting it to string and one image may take 500kb or 600kb something and after that when i collectively convert this to json string using gson getting outofmemory error. – KJEjava48 Apr 19 '17 at 05:53
  • @KJEjava48 I don't know, how I can help. Try to avoid json when sending images – Roman Truba Apr 24 '17 at 14:22
1

To encode the bimap to image:

 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
   byte[] imageBytes = byteArrayOutputStream.toByteArray();
   String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    Log.d("bytearray", String.valueOf(byteArrayOutputStream.toByteArray()));
    Log.d("encodedimage",encodedImage);
Hanisha
  • 849
  • 10
  • 8
0

note that if you get the base64 string from other process like JSInterface, the string will start with the base64's header like 'data:image/png;base64,', you need to cut it off if you use BitmapFactory.decodeByteArray to decode it.

String dataStr = thumb.startsWith("data:image") ? thumb.substring(thumb.indexOf(',') + 1) : thumb;
byte[] decodedString = Base64.decode(dataStr, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
tigerZ
  • 169
  • 1
  • 7