How to load image stored in byte array into WebView?

everything! I compressed a lot of images in the "pictures.zip" file. I want to load one of these pictures into a WebView as follows:

WebView wv = (WebView)findViewById(R.id.WebView01); wv.loadDataWithBaseURL(null,"<img src=\"abc.jpg\">", "text/html", "UTF-8", null); 

Here "abc.jpg" is an image compressed into a picture.zip file.

  • I just want to unzip the image from the zip file and get the image byte stream, and then load the image into the WebView from the byte stream.

  • I do not want to unpack the image from the zip file, and then save it to the SD card, and then upload it.

  • In addition, I do not want to encode the byte to the base64, and then upload the image to WebView either because these two ways are very slow.

+7
source share
2 answers

As far as I know, all three of these requirements do not exist. Base64 encoding it and loading it directly into an image tag is probably best if you don't want to write it to storage, although you can still write it to internal storage and display it in a webview.

 private static final String HTML_FORMAT = "<img src=\"data:image/jpeg;base64,%1$s\" />"; private static void 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"); } 
+6
source

I recommend using the built-in HTTP listener in your application, where a specific port is listening (for example, 8001), and then in the images of your HTML page to your listener. For example, a Test.png search would look something like this:

http: // localhost: 8001 / Test.png

This request will go to your listener, where you can look into your zip file or database, and then return the byte stream to the stream of HTTP responses!

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

Hope this helps :-)

+1
source

All Articles