Lipis
Lipis

Reputation: 21835

How can I check if the given URL of an image exists using 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.

Upvotes: 8

Views: 2585

Answers (3)

liwston
liwston

Reputation: 113

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

Upvotes: 1

Micer
Micer

Reputation: 8979

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.");
    }
});

Upvotes: 10

Jason Hall
Jason Hall

Reputation: 20930

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.

Upvotes: 5

Related Questions