Skip to content

Instantly share code, notes, and snippets.

@pfieffer
Last active June 5, 2018 06:26
Show Gist options
  • Save pfieffer/090e73fefc93a787d4c9b2eef8be5625 to your computer and use it in GitHub Desktop.
Save pfieffer/090e73fefc93a787d4c9b2eef8be5625 to your computer and use it in GitHub Desktop.
A utility class to convert Bitmap image to Base64 and vice versa.
public class ImageUtil {
/**
*
* @param base64Str Base64 string for the image.
* @return Bitmap value for the image.
* @throws IllegalArgumentException
*/
public static Bitmap convert(String base64Str) throws IllegalArgumentException {
byte[] decodedBytes = Base64.decode(
base64Str.substring(base64Str.indexOf(",") + 1),
Base64.DEFAULT
);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
/**
*
* @param bitmap Bitmap image to be converted to base64
* @return Base64 String for the bitmap passed as paramater
*/
public static String convert(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment