loading an image

916 views
Skip to first unread message

tanteanni

unread,
Jan 9, 2012, 7:40:36 AM1/9/12
to google-we...@googlegroups.com
i try to get an image displayed that is retrieved from an url. my first (false) attempt was not respecting the asynchronous behavior so it only worked in hosted mode:
"
Image image = new Image(someURL);
view.setImage(image);
"
but this failed in "real" mode. so i googled around (besides wrong examples and this ) i found the problem: a have to listen to load event. So i changed my code to this:
"
        image = new Image(url);
        image.addLoadHandler(new LoadHandler() {

            @Override
            public void onLoad(LoadEvent event) {
                view.setImage(image);
                image.setVisible(true);
            }
        });
"
the problem is that the event is never been fired or at least onLoad is never executed. I also tried to addHandler before setting url. The url it self works fine (browser is displaying the image) and log shows positive image response: 
"
response : HTTP/1.1 200 
Content-Type: image/png


graphdata : [B@12505f5
"

so how to correctly load/display an image in gwt?

thx in advance

Derek

unread,
Jan 9, 2012, 9:44:17 AM1/9/12
to Google Web Toolkit
The loadhandler won't fire until the image is added to the DOM. So for
example, you could put view.setImage(image) outside the loadhandler,
and GWT.log("loaded!") inside the loadhandler, and then you should see
"loaded!" in your dev console.

But a loadhandler is not required for loading an image, it's only if
you want to respond to the image being loaded. For example, I'm
working on a GWT app that shows a progress bar until an image gets
loaded, and for that code, I put in a loadhandler on the image that
removes the progress bar in the onLoad() (and I also have an
errorhandler that displays a message and remove the progress bar if
the image fails to get loaded). But if you just want to load an image,
you only need to set the source and add it to the page and it will
load.

I don't actually see anything wrong with your first attempt. Why did
it fail outside of hosted mode?

If you want to load your image ahead of time so it displays
immediately, try Image.prefetch(url).

On Jan 9, 7:40 am, tanteanni <tantea...@hotmail.com> wrote:
> i try to get an image displayed that is retrieved from an url. my first
> (false) attempt was not respecting the asynchronous behavior so it only
> worked in hosted mode:
> "
> Image image = new Image(someURL);
> view.setImage(image);
> "
> but this failed in "real" mode. so i googled around (besides wrong examples<http://examples.roughian.com/index.htm#Listeners~LoadListener> and
> this <http://code.google.com/p/gwt-image-loader/> ) i found the problem: a

tanteanni

unread,
Jan 10, 2012, 3:35:52 AM1/10/12
to google-we...@googlegroups.com
thx,

but my problem is, that it fails outside hosted mode. in hosted mode the image is loading, in real mode the small destroyed image icon appears (that appears in browser if image is not available) at the moment i press the button to load the image. and as i said before: if i load the image url directly the image loads fine.

Derek

unread,
Jan 10, 2012, 8:58:26 AM1/10/12
to Google Web Toolkit
I'd be surprised if that's related to GWT, but maybe I'm wrong.

Are you sure you're using the right URL? If you're using a relative
path, you might get tripped up between hosted and compiled mode. If
you're using the url "/myimage.png", then hosted mode will probably
work and real mode would probably fail (assuming you set up your
servlet as "/myimage.png"), since real mode would probably actually be
at "/myproject/myimage.png" or something like that.

I would grab some developer tools and inspect your image source and
make sure that the url it's going to actually works. Also look for any
other errors that might be on the console.

Hope that helps,

Derek

tanteanni

unread,
Jan 11, 2012, 3:36:19 AM1/11/12
to google-we...@googlegroups.com
thats a good point (i am using a relative url). i will look into it - thx!

tanteanni

unread,
Jan 11, 2012, 4:51:49 AM1/11/12
to google-we...@googlegroups.com
As assumed you were right: the called image url is somthing like this "/imageRetriever?[some Parameters] (its a simple HttpServlet). So in hosted and webmode if i add /imageRetirever... to Url in Browser the Image is displayed fine. But as you said in WebMod the Url contains a "myProject"-Part. But how to fix this? 

Derek

unread,
Jan 11, 2012, 9:46:06 AM1/11/12
to Google Web Toolkit
GWT.getHostPageURL()+"imageRetriever"

Javadocs suggest using getModuleBaseURL() instead of getHostPageURL(),
so that would be GWT.getModuleBaseURL()+"../imageRetriever", but in
the absence of cross-site loading, I don't think it makes much
difference.

Derek

tante anni

unread,
Jan 12, 2012, 10:57:49 AM1/12/12
to google-we...@googlegroups.com

thx in meantime i tried just this
but only 
GWT.getHostPageURL()+"imageRetriever" is working properly. "
GWT.getModuleBaseURL() " doesn't work neither in web nor in hosted mode. the modules name differs from actual url: module "myapp" url: "MyApp-0.0.1"(in web mode) an and "MyApp.html?gwt.codesvr=127.0.0.1:9997" (in hosted mode). So 
GWT.getModuleBaseURL()  must always be wrong

----------------------------------------
> Date: Wed, 11 Jan 2012 06:46:06 -0800
> Subject: Re: loading an image
> From: derek...@gmail.com
> To: google-we...@googlegroups.com


>
> GWT.getHostPageURL()+"imageRetriever"
>
> Javadocs suggest using getModuleBaseURL() instead of getHostPageURL(),
> so that would be GWT.getModuleBaseURL()+"../imageRetriever", but in
> the absence of cross-site loading, I don't think it makes much
> difference.
>
> Derek
>

> On Jan 11, 4:51 am, tanteanni wrote:
> > As assumed you were right: the called image url is somthing like this
> > "/imageRetriever?[some Parameters] (its a simple HttpServlet). So in hosted
> > and webmode if i add /imageRetirever... to Url in Browser the Image is
> > displayed fine. But as you said in WebMod the Url contains a
> > "myProject"-Part. But how to fix this?
>

> --
> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
>

Reply all
Reply to author
Forward
0 new messages