Skip to content Skip to sidebar Skip to footer

How To Load An Image Stored In A Byte Array To Webview?

everyone! I have compressed lots of pictures to a 'pictures.zip' file. I want to load one of these pictures to a WebView like this: WebView wv = (WebView)findViewById(R.id.WebView

Solution 1:

As far as I know, there is no way to have all three of these requirements. Base64 encoding it and loading it into the image tag directly is probably your best bet if you don't want to write it to storage, although you can still write it to internal storage and show it in a webview.

privatestaticfinalString HTML_FORMAT = "<img src=\"data:image/jpeg;base64,%1$s\" />";

privatestaticvoid openJpeg(WebView web, byte[] image)
{
    String b64Image = Base64.encode(image, Base64.DEFAULT);
    String html = String.format(HTML_FORMAT, b64Image);
    web.loadData(html, "text/html", "utf-8");
}

Solution 2:

I recommend using embeddable HTTP listener within your application where listen to specific port (for instance 8001), then in your HTML Page reference images to your listener. For example looking for Test.png would be something like:

http://localhost:8001/Test.png

This request will end up in your listener where you can look into your zip file or database and then return byte stream to HTTP Response stream!

I really recommend you to take a look at NanoHTTPD (http://elonen.iki.fi/code/nanohttpd/) and try to implement custom serve method for your goal.

I hope this helps :-)

Post a Comment for "How To Load An Image Stored In A Byte Array To Webview?"