Encode and decode bitmap object in base64 string in Android

AndroidBitmapBase64

Android Problem Overview


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;
}

Android Solutions


Solution 1 - Android

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);

Solution 2 - Android

Hope this will help you

 Bitmap bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));

(if you are referencing URI to construct bitmap) OR

Resources resources = this.getResources();
Bitmap bitmap= BitmapFactory.decodeResource(resources , R.drawable.logo);

(if you are referencing drawable to construct bitmap)

Then Encode it

 ByteArrayOutputStream stream = new ByteArrayOutputStream();  
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
 byte[] image = stream.toByteArray();
 String encodedImage = Base64.encode(image, Base64.DEFAULT);

For Decoding Logic will be exactly reverse of encoding

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

Solution 3 - Android

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);

Solution 4 - Android

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreaFView Question on Stackoverflow
Solution 1 - AndroidRoman TrubaView Answer on Stackoverflow
Solution 2 - AndroidVipulView Answer on Stackoverflow
Solution 3 - AndroidHanishaView Answer on Stackoverflow
Solution 4 - AndroidtigerZView Answer on Stackoverflow