简体   繁体   中英

In Android, how do I check if the URL being loaded in a webview is an image?

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM