How to detect quickly if a specified URL contains an image in Android?
I have link of type http://foo.bar/w23afv
so naive approach like checking end of URL string won't work here.
To check if a url is an image, call the test() method on a regular expression that matches an image extension at the end of a string, e.g. . png or . jpg . The test() method will check if the url ends with an image extension and will return true if it does.
Use URLUtil to validate the URL as below. It will return True if URL is valid and false if URL is invalid.
Check if the HTTP Content-Type
response header starts with image/
.
URLConnection connection = new URL("http://foo.bar/w23afv").openConnection();
String contentType = connection.getHeaderField("Content-Type");
boolean image = contentType.startsWith("image/");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With