8

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.

Lipis
  • 20,734
  • 19
  • 92
  • 118

3 Answers3

10

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.");
    }
});
Micer
  • 8,158
  • 3
  • 78
  • 70
5

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.

Jason Hall
  • 20,231
  • 4
  • 48
  • 56
1
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
liwston
  • 93
  • 7