如何將存儲在字節數組中的圖像加載到 WebView? (How to load an image stored in a byte array to WebView?)


問題描述

如何將存儲在字節數組中的圖像加載到 WebView? (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.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.

‑‑‑‑‑

參考解法

方法 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.

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");
}

方法 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 :‑)

(by Chao WeijakebasileQorbani)

參考文件

  1. How to load an image stored in a byte array to WebView? (CC BY‑SA 3.0/4.0)

#bytearray #webview #Android






相關問題

更改創建 XML 閱讀器時使用的 XmlDictionaryReaderQuotas 對象的 MaxArrayLength 屬性 (Changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader)

將文件讀入 4 個字節的 ByteArrays (Read file into ByteArrays of 4 bytes)

使用套接字 (C#) 進行文件傳輸 - 接收到的文件不包含完整數據 (File transfer using sockets (C#) - received file doesn't contain full data)

將 Java 字節讀取器轉換為 InputStream (Converting a Java byte reader to an InputStream)

用 bigInteger 和其他方法重建字節數組 (Rebuild byte array with bigInteger and other method)

如何將字節數組作為參數發送到 HTML.Action? (How to send byte array as paramater to HTML.Action?)

如何將存儲在字節數組中的圖像加載到 WebView? (How to load an image stored in a byte array to WebView?)

我希望將畫布保存為 mySql blob 字段中的 blob (I am looking to save a canvas as a blob in mySql blob field)

將字節數組的部分轉換為整數值 (Converting sections of byte arrays to integer values)

從內存中釋放 jni 引用 (Freeing jni references from memory)

為什麼 JPGEncoded bytearray 從 AS3 發送到 AMFPHP 會導致圖像無效? (Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image?)

Java:字節到浮點數/整數 (Java: Bytes to floats / ints)







留言討論