Reputation: 21835
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
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
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
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