5

I am getting a base64 byte[] from an xml file via jaxb and I am not sure how to convert this back to an gwt image (which is basically an underlying html img if I understood that correctly). How do I convert into the proper string?

My first instinct was to

public void onSuccess(final byte[] icon) {
img.setUrl("data:image/png;base64,"+icon.toString());

but obviously that does not work. Any help is appreciated!

Hoax
  • 1,004
  • 2
  • 15
  • 31

3 Answers3

6

If you want to use data URIs (with base64 encoding) - although IE <=7 doesn't support it, and IE8 only allows up to 32 kB - you'll have to base64-encode the image data.

There are several Base64 encoders around e.g. com.google.gwt.user.server.Base64Utils, which you can use on the server side:

String base64 = Base64Utils.toBase64(icon);

Then transfer the encoded data to the client.

If you absolutely want to, you could also use the encoder on the client side, but that would require to copy the java file to the client source (if you make sure that the implementation you choose allows that).

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
4
String b = "this should be a base64 encode string that was generated from an icon or byte[]";
Image image = new Image();
image.setUrl("data:image/png;base64,"+b);

view.getPreviewTable().setWidget(14, 0, image);

the Image is a gwt image in this case.

chown
  • 51,908
  • 16
  • 134
  • 170
0

You should supply the URL which can be used to get the image. I really don't think your code will result in something that looks like a url (something like, http://localhost/myimage.png, or maybe mywebapp/myimage.png...)

markovuksanovic
  • 15,676
  • 13
  • 46
  • 57
  • The OP is talking about data URIs. The format is valid: http://en.wikipedia.org/wiki/Data_URI_scheme#Format (note, that it doesn't work with all browsers) – Chris Lercher Jun 19 '10 at 15:41