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
  • 21,388
  • 20
  • 94
  • 121

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,731
  • 3
  • 79
  • 73
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,632
  • 4
  • 50
  • 57
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
  • 113
  • 8