Display an Image from JPA entity.

247 views
Skip to first unread message

Eric

unread,
Dec 26, 2010, 9:37:05 AM12/26/10
to Google Web Toolkit
Hello.
I'm trying to figure out how to display Images using the
requestFactory.
I have an entity that represents an Image and I want to have a simple
list of all images.
I'm wondering what data type should I use for holding the image data.
The RequestFactory docs mention that primitives are not allowed
however
if I create an entity like this one:
@Entity
public class Picture {
...

@Lob
@Basic(fetch=javax.persistence.FetchType.LAZY)
private Byte[] data;
...
}

I'm getting runtime exception saying Byte is not a primitive type.

What would be the best way to put JPA entities. requestFactory and
UiBinder together?

Tobias

unread,
Dec 26, 2010, 1:45:51 PM12/26/10
to google-we...@googlegroups.com
Hello,

I think it would be easiest not to transfer the actual image data with RequestFactory. Just provide the Image-URI within the entities transferred by RequestFactory and have it point to a special servlet that serves the actual images. This way you can also use the AppEngine Bloblstore (if you're running on AppEngine). I'm not sure you can do anything useful with byte[] data on the client anyway, the only way to directly transmit the image using RequestFactory would imho be to convert it to a Base64 string (which you use as a CSS background-image to display it).

Regards,
Tobias

Matthew Hill

unread,
Dec 26, 2010, 4:06:11 PM12/26/10
to google-we...@googlegroups.com
You could either store the URL of the uploaded image or store the base-64 encoded data of the image in your database.

Eric

unread,
Dec 27, 2010, 6:32:27 AM12/27/10
to Google Web Toolkit
Thank you
I tried the URL approach, it works great in development mode but when
deployed on tomcat the location of the uploaded file is changed for
some reason. it puts everything in tomcat's bin(DUH!).
I'll try the base64 way.
I'm using uibinder image this way:
@UiField Image

Image has only setters for path And I cannot counstruct ImageResource
from raw data.
How can I set the image data from the base64 field of my entity?

Eric

unread,
Dec 27, 2010, 8:22:36 AM12/27/10
to Google Web Toolkit
Unfortunately,the following code produces a broken image in the
browser.
imagePath.setUrl("data:image/png;base64,"+proxy.getImageData());

I can't figure out what is wrong with this since nothing, including
chrome developer tools, shows any error message.


RequestURL:data:image/
png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QAAAAAAAD5Q7t_AAAACXBI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x_b56rVYUh5mzhNLoIhGwgY4UVy6a
$ZGtD2ld2AwnWBnNHnyYQZd7hvn5_uNzvpzc$_UQQLwaFw2WIIsxHe$Y
$P0KAeIJmm0uyjQbWOWyjQZAk2CQhEME6R6bRIK_GnoyjejYzbxNN6sh5h1eHFUfGJzSr4x5x
$ChCrIlApwgGBqjDPw6cAMgBuUvDvGmO9u5D_cJbgyxTI60FcMaMefQ1mPvQD4Gm1IPUAXzqLvU7n1z_1f4GWK3Nq3bFgzYAAAAASUVORK5CYII=
Request Headers
Referer:http://127.0.1.1:8888/ApplicationScaffold.html?
gwt.codesvr=127.0.1.1:9997
User-Agent:Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/
534.10 (KHTML, like Gecko) Ubuntu/10.10 Chromium/8.0.552.224 Chrome/
8.0.552.224 Safari/534.10
Reply all
Reply to author
Forward
0 new messages