问题
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, returnnull
to tell theWebView
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 anInputStream
to read the JPEG/PNG image bytes. - Return a
WebResourceResponse
with the mime type (i.e. image/jpeg) and theInputStream
you created to read the image data. - Call
setWebViewClient
on the webview with an instance of yourWebViewClient
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