2

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.

shengbinmeng
  • 1,517
  • 2
  • 12
  • 22

1 Answers1

3

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.

Community
  • 1
  • 1
kris larson
  • 30,387
  • 5
  • 62
  • 74
  • Looks like your best bet for decoding TIFF is using JNI, here's a project you can look at: https://code.google.com/p/tiffonandroid/ – kris larson Apr 09 '15 at 11:26
  • It seems very likely to be the right way! Thank you. I will try this and let you know how it works. – shengbinmeng Apr 09 '15 at 11:44