如何在Android中快速检测特定URL是否包含图像?
我有如下类型的链接 http://foo.bar/w23afv
,因此简单地检查URL字符串的结尾将无法起作用。
检查HTTP响应头中Content-Type
是否以image/
开头。
URLConnection connection = new URL("http://foo.bar/w23afv").openConnection();
String contentType = connection.getHeaderField("Content-Type");
boolean image = contentType.startsWith("image/");