0

Checking the extension of the file works in most cases, but not for all. Consider the following link:

http://t0.gstatic.com/images?q=tbn:ANd9GcTqqcX4SsdwgithvRcjQl_W3Hj-eIc_12FvdeGS2sO31uZ0F1HHqcdRKNo

Can I check from the URL if it is a link to an image? If not, is there a way to check after the page has loaded in the webview?

I know that I could download every page using HttpURLConnection, but I'm trying to avoid that approach.

Pawan
  • 73
  • 3
  • 10
  • 2
    "Can I check from the URL if it is a link to an image?" -- no. "is there a way to check after the page has loaded in the webview?" -- not that I am aware of. Determining the MIME type of the current content seems to be difficult [even in JavaScript](http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript). – CommonsWare Jul 25 '15 at 22:33

1 Answers1

1

Check Response HTTP Header Content-Type. It will be one of the image types. For Example, if I open the URL you have provided in Browser, will see Content-Type as show below

Content-Type:image/jpeg

You can use HttpHead method to fetch just the headers

enter image description here

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • AFAIK, there's no way to obtain HTTP headers in a WebView. – Pawan Jul 25 '15 at 22:18
  • Sorry, did not know about that. Will this help - http://stackoverflow.com/questions/3134389/access-the-http-response-headers-in-a-webview or is that something you already knew and did not want to do – Wand Maker Jul 25 '15 at 22:25
  • I had read that one. I was hoping to find a better way, seeing that it has been 5 years since that question. That's a very long time in Android years. :) – Pawan Jul 26 '15 at 13:00
  • Another option is to use http://developer.android.com/reference/org/apache/http/client/methods/HttpHead.html – Wand Maker Jul 26 '15 at 13:01
  • Thanks. Since I'd have to request at least once anyway, the solution to http://stackoverflow.com/a/3134609/1176470 should be the better approach. – Pawan Jul 26 '15 at 13:05