Detect if specified url is an image in Android?

2019-03-26 20:00发布

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.

1条回答
放荡不羁爱自由
2楼-- · 2019-03-26 20:43

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/");
查看更多
登录 后发表回答