0

I want to convert bitmap to base64 to store in sharedpreference. Whenever i am trying to convert it to base64 getting black bitmap not formatted in good shape.

Here is Code for Converting Bitmap to base64.

                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            b123.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                            byte[] b = baos.toByteArray();

                            encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

Thanks for Help in Advance

2 Answers2

2

Try this:

try{
   byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
   Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
   return bitmap;
 }catch(Exception e){
   e.getMessage();
   return null;
 }
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
2

You are compressed image in JPEG please use PNG and try to convert in base64.

 b123.compress(Bitmap.CompressFormat.PNG, 100, baos);
MIkka Marmik
  • 1,101
  • 11
  • 29