user1026063
user1026063

Reputation: 1

compress bitmap to string android

Hi guys I have a problem when I go to encode the image to a bitmap and then do a comparison with the decode an encoded image with tools via the Internet but does not give me the same decoding and can not figure out why.

String path = "/sdcard/bluetooth/bluetooth.png"; 
Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

Upvotes: 0

Views: 1006

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

I used the below for encoding and decoding and it works fine for me

Encode

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

Try the below for decoding and set the resulting bitmap to imageview and check with the original.

Decode.

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

Upvotes: 1

Related Questions