How to convert bitmap to PNG and then to base64 in Android? How to convert bitmap to PNG and then to base64 in Android? android android

How to convert bitmap to PNG and then to base64 in Android?


Try this to convert bitmap into png:

 bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);

Check method's documentation.

You can directly convert bitmap to Base64. Use this for encoding and decoding from and to Base64.

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;}public static Bitmap decodeBase64(String input) {    byte[] decodedByte = Base64.decode(input, 0);    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); }