Convert byte[] to Base64 string for data URI Convert byte[] to Base64 string for data URI arrays arrays

Convert byte[] to Base64 string for data URI


I used this and it worked fine (contrary to the accepted answer, which uses a format not recommended for this scenario):

StringBuilder sb = new StringBuilder();sb.append("data:image/png;base64,");sb.append(StringUtils.newStringUtf8(Base64.encodeBase64(imageByteArray, false)));contourChart = sb.toString();


According to the official documentation Base64.encodeBase64URLSafeString(byte[] binaryData) should be what you're looking for.

Also mime type for JPG is image/jpeg.


That's the correct syntax. It might be that your web browser does not support the data URI scheme. See Which browsers support data URIs and since which version?

Also, the JPEG MIME type is image/jpeg.