Skip to content Skip to sidebar Skip to footer

Can I Make Android Webview Support Other Image Formats (e.g. Tiff)?

For example, how to make the following HTML page actually display the image, in an Android WebView?

Solution 1:

I haven't tried this, but this is what I would do:

  • Make a subclass of WebViewClient
  • Override shouldInterceptRequest() to check the URL and see if a TIFF was requested. If it was not a TIFF, return null to tell the WebView to handle the request itself.
  • If a TIFF was requested, open HttpURLConnection to the TIFF url and read the data, convert the TIFF to a JPEG or PNG ex. How to convert TIFF to JPEG/PNG in java and set up an InputStream to read the JPEG/PNG image bytes.
  • Return a WebResourceResponse with the mime type (i.e. image/jpeg) and the InputStream you created to read the image data.
  • Call setWebViewClient on the webview with an instance of your WebViewClient subclass.

Rather than converting on the device using a JNI library, I think I would convert the image on a server and open the HttpURLConnection to the pre-converted image stream, i.e. http://example.com/convert_tiff?url=http%3A%2F%2Fwww.alternatiff.com%2Fsample.tif&fmt=JPEG and then return that InputStream in the WebResourceResponse. I guess it depends on how cheap the bandwidth and server resources are for you.

Post a Comment for "Can I Make Android Webview Support Other Image Formats (e.g. Tiff)?"