Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if the given URL of an image exists using GWT?

Tags:

url

image

gwt

I want to check if a given URL exists and it's an image, in order to create a new Image(String url) from it. If the given URL is not an image then it should return an error.

like image 725
Lipis Avatar asked Jul 25 '10 15:07

Lipis


3 Answers

You could do this with a RequestBuilder -- just request the image URL, use the Response's getHeaders() method to get the content type, and check if it's an image.

like image 44
Jason Hall Avatar answered Oct 09 '22 22:10

Jason Hall


I was looking for the same thing - I wanted to determine when image is not loaded from URL. There is an ErrorHandler for this purpose. Here's the code:

Image img = new Image("some_url/img.jpg");
img.addErrorHandler(new ErrorHandler() {                
    @Override
    public void onError(ErrorEvent event) {
        System.out.println("Error - image not loaded.");
    }
});
like image 107
Micer Avatar answered Oct 09 '22 23:10

Micer


Image img = new Image(); //no url parameter
img.addErrorHandler(new ErrorHandler() {                
    @Override
    public void onError(ErrorEvent event) {
        System.out.println("Error - image not loaded.");
    }
});
img.setUrl("some_url/img.jpg"); // set the url after handler
like image 26
liwston Avatar answered Oct 10 '22 00:10

liwston