我可以让Android的WebView支持其他图像格式(如TIFF)? [英] Can I make Android WebView support other image formats (e.g. TIFF)?
问题描述
例如,如何使以下的HTML页面实际显示图像,在Android的WebView?
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>
任意(黑客)的建议是AP preciated。我只是一个应用程序开发者和无法控制整个系统,所以修改操作系统源$ C $ c是不适用的。
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.
推荐答案
我还没有试过,但是这是我会做什么:
I haven't tried this, but this is what I would do:
- 请的一个子类
WebViewClient
- 覆盖
shouldInterceptRequest()
查询的网址,看是否请求了TIFF。如果它不是一个TIFF,返回空
来告诉的WebView
来处理该请求本身。 - 如果请求了TIFF,打开
HttpURLConnection类
来的TIFF URL和读取数据时,TIFF转换为JPEG或PNG的ex。如何TIFF转换成JPEG / PNG在Java 并成立了一个的InputStream
来读取JPEG / PNG图像的字节数。 - 返回一个
WebResourceResponse
的MIME类型(即图像/ JPEG格式),的InputStream
创建阅读图像数据。 - 呼叫
setWebViewClient
与你的WebViewClient
子类的实例web视图。
- 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.
而不是使用JNI库设备上的转换,我想我会的图像转换的服务器上,并打开HttpURLConnection类为pre转换的图像流,即<一个href=\"http://example.com/convert_tiff?url=http%3A%2F%2Fwww.alternatiff.com%2Fsample.tif&fmt=JPEG\" rel=\"nofollow\">http://example.com/convert_tiff?url=http%3A%2F%2Fwww.alternatiff.com%2Fsample.tif&fmt=JPEG然后返回的InputStream
在 WebResourceResponse
。我想这取决于带宽和服务器资源多么便宜的为您服务。
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.
这篇关于我可以让Android的WebView支持其他图像格式(如TIFF)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!