Last active
June 5, 2018 06:26
-
-
Save pfieffer/090e73fefc93a787d4c9b2eef8be5625 to your computer and use it in GitHub Desktop.
A utility class to convert Bitmap image to Base64 and vice versa.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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