Detect if specified url is an image in Android?

前端 未结 1 2055
误落风尘
误落风尘 2021-02-08 11: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

相关标签:
1条回答
  • 2021-02-08 11:47

    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/");
    
    0 讨论(0)
提交回复
热议问题