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

拥有回忆 提交于 2019-12-30 08:36:33

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2941009/android-easiest-way-to-make-a-webview-display-a-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!