7

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.WebView01);
wv.loadDataWithBaseURL(null,"<img src=\"abc.jpg\">", "text/html", "UTF-8", null);

Here,"abc.jpg" is a picture that has been compressed to pictures.zip file.

  1. I just want to decompress the picture from the zip file and get the picture's byte stream, and then load the image to the WebView from the byte stream.

  2. I don't want to decompress the picture from the zip file and then save it to sdcard and then load it.

  3. Moreover, I don't want to encode the pitcture's byte to a base64 and then load the image to the WebView either, because these two ways will be very slow.

Chao Wei
  • 71
  • 1
  • 4
  • @user549380: First, why do you need to use a WebView? Second, your logic isn't right here - using "" implies reference to a 'file' of some sort local or otherwise. As far as I've read so far, you can't create 'in-memory' files with android and if you could, I don't see how a WebView could access them. – Squonk Dec 21 '10 at 03:17
  • A famous software called "mdict" can load image from its resource file(.mdd file) without storing the image into harddrive. And it just use html like to load abc.jpg which is compressed to a .mdd file. – Chao Wei Dec 21 '10 at 05:59
  • Show an example of how mdict uses and it might explain things further. – Squonk Dec 21 '10 at 08:55
  • By [Overriding webviewClient][1] you can achieve this but it requires android level 10+ [1]: http://stackoverflow.com/questions/4780899/intercept-and-override-http-requests-from-webview – duckduckgo Aug 25 '13 at 13:37

2 Answers2

6

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.

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");
}
jakebasile
  • 8,084
  • 3
  • 28
  • 34
  • 4
    I would like to note that you could shorten this further (an probably increase performance) by giving loadData() the base64 directly; `String b64Image = Base64.encodeToString(image, Base64.DEFAULT);` `web.loadData(b64Image, "image/jpeg", "base64");` – Thorbear Aug 02 '12 at 08:08
1

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 :-)

Qorbani
  • 5,825
  • 2
  • 39
  • 47
  • this is good option for some situations but accessing http://localhost is another headache it doesnt work like PC – duckduckgo Aug 25 '13 at 06:28