Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if specified url is an image in Android?

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.

like image 545
pixel Avatar asked Aug 10 '10 21:08

pixel


People also ask

How do you check if a URL is an image?

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.

How can I check if an android URL is valid?

Use URLUtil to validate the URL as below. It will return True if URL is valid and false if URL is invalid.


1 Answers

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/");
like image 74
BalusC Avatar answered Oct 16 '22 07:10

BalusC