Open top menu

How to convert image to String and String to Image ?


In this blog we will explain how to convert Image to String Base64 and Base64 String encode to Image . Create Bitmap and set the image  and you have image convert in byte array.
String StrImag = "";
 
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);

StrImag = bitmapToBase64(bm);
 
 
//canvert Bitmap image to string................................ 

private String bitmapToBase64(Bitmap bitmap) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

    byte[] byteArray = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);

}
 

convert Image to String Base64 and Base64 String encode to Image.
byte[] decodedString = Base64.decode(StrImag, Base64.DEFAULT);

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

holder.imgDamage.setImageBitmap(decodedByte);

0 comments