Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to Display a Bitmap in a WebView?

I'm using a WebView for displaying a map-like image, due to the built-in panning and zooming functionality. However, I need to overlay some other information (markers etc) at certain points on the map image, and display all of this.

So I take my base map image as a Bitmap, and overlay my other images over that (using Canvas and more Bitmaps), giving me a final Bitmap with all the info. Because I need to edit the image at runtime, I can't use a pre-made file. Implementing the overlaying of bitmaps is not the problem, the problem is that I don't know how to easily display the resulting image with pan and zoom capabilities.

Is there a way to display a Bitmap using a WebView? Or any other suggestions?

like image 729
breadbin Avatar asked Jun 01 '12 11:06

breadbin


1 Answers

Using the link from itsrajesh4uguys, I've created this code snippet:

// Desired Bitmap and the html code, where you want to place it
Bitmap bitmap = YOUR_BITMAP;
String html="<html><body><img src='{IMAGE_PLACEHOLDER}' /></body></html>";

// Convert bitmap to Base64 encoded image for web
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
    String image = "data:image/png;base64," + imgageBase64;

// Use image for the img src parameter in your html and load to webview
html = html.replace("{IMAGE_PLACEHOLDER}", image);
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "");
like image 104
peter.bartos Avatar answered Oct 18 '22 01:10

peter.bartos