Android: Easiest way to make a WebView display a Bitmap?
Asked Answered
A

2

8

I have some images that I loaded from a remote source stored in Bitmap variables and I want to display them. In addition to switching between these images the user should also be able to zoom and pan them. My first idea was to somehow pass them via an intent to the built-in gallery application but this doesn't seem to be possible. A solution that is suggested in several places is using a WebView since it already supports zooming and panning. My question is how does my Bitmap data get into the WebView? Do I have to write it to a file first, which I would have to remove again later, or is there an easier way?

Or are there even better ways to accomplish my main goal, which is displaying Bitmap data as zoomable and panable images?

Aromaticity answered 31/5, 2010 at 1:47 Comment(0)
A
0

I wasn't satisfied with WebView after all so I ended up creating my own image viewing Activity. Further descriptions on how I did it can be found in this post on google groups.

Aromaticity answered 15/6, 2010 at 12:18 Comment(0)
S
4

You can just use webview to directly view your image remotely. You do not need to save anymore the image in a file. Here is a sample code snippet.

myWebView.getSettings().setBuiltInZoomControls(true); //to get zoom functionalities

String url = "http://....."; //url of your image

String x= "<html><head><meta name=\"viewport\" content=\"width=device-width, minimum-scale=1.0\"/><style type=\"text/css\">html, body {margin: 0;padding: 0;} img {border: none;}</style><head><body style=\"background: black;\"><table><tr><td align=\"center\"><img src=\"" + url + "\" /></td></tr></table></body></html>";

myWebView.loadData(x, "text/html", "UTF-8");

About switching images, you can just change the value of the url and call the loadData again of the webview.

Sheehan answered 31/5, 2010 at 6:42 Comment(2)
The reason why I want to pass Bitmap data that I already loaded is that this way my application can cache the image (in my case in the database) so it will only have to be loaded once.Aromaticity
As far as I know, webview has already a caching feature. You can override its behavior by specifying its cache mode. myWebView.getSettings().setCacheMode(mode); You can refer to this link. developer.android.com/intl/de/reference/android/webkit/… You may use the 'LOAD_CACHE_ELSE_NETWORK' as the mode wherein it uses cache if content is there, even if expired else, load from network. Another way is to manually handle the zooming, panning and caching by yourself without using webview. You can look at the code ViewImage.java under camera package of the android source code.Sheehan
A
0

I wasn't satisfied with WebView after all so I ended up creating my own image viewing Activity. Further descriptions on how I did it can be found in this post on google groups.

Aromaticity answered 15/6, 2010 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.