Tuesday 21 February 2017

Converting the bitmap to base64 string and base64 string to bitmap image in android

/* bitmap code use in main activity*/
Bitmap profileimage = BitmapFactory.decodeFile(you image path here);
String basestring=convert(bitmapConvert)

/*converting the base64 string to bitmap */
public Bitmap convert(String base64Str) throws IllegalArgumentException {
        byte[] decodedBytes = Base64.decode(
                base64Str.substring(base64Str.indexOf(",") + 1),
                Base64.DEFAULT        );
        return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
    }
  

/*converting the bitmap image to string in base64*/ 
 public String convert(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG70, outputStream);
        return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
    }