Can I make Android WebView support other image formats (e.g. TIFF)?

拜拜、爱过 提交于 2019-12-24 10:47:11

问题


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

<html>
<body>
<img src="http://www.alternatiff.com/sample.tif">
</body>
</html>

Any (hacking) suggestion is appreciated. I am only an app developer and cannot control the whole system, so modifying the OS source code is not applicable.


回答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.



来源:https://stackoverflow.com/questions/29534120/can-i-make-android-webview-support-other-image-formats-e-g-tiff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!