Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Easiest way to make a WebView display a Bitmap?

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?

like image 611
Steven Meliopoulos Avatar asked May 31 '10 01:05

Steven Meliopoulos


1 Answers

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.

like image 159
capecrawler Avatar answered Sep 23 '22 06:09

capecrawler