Tuesday, 19 April 2016

Encode and Decode the Bitmap to Base64 String and vice versa

// Encode the Bitmap to Base64 String:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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;
    }

Decode the Base64 String to Bitmap:

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

No comments: