如何将存储在字节数组中的图像加载到WebView? [英] How to load an image stored in a byte array to WebView?

查看:100
本文介绍了如何将存储在字节数组中的图像加载到WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!我已经将许多图片压缩为"pictures.zip"文件.我想像这样将这些图片之一加载到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);

在这里,"abc.jpg"是已被压缩为pictures.zip文件的图片.

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

  1. 我只想从zip文件中解压缩图片并获取 图片的字节流,然后 从中将图像加载到WebView 字节流.

  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.

我不想解压缩 压缩文件中的图片,然后 将其保存到sdcard,然后加载.

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

此外,我不想编码 pitcture的字节到base64,然后 将图像加载到WebView 要么,因为这两种方式将 会很慢.

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.

推荐答案

据我所知,没有办法满足所有这三个要求.如果您不想将其写入存储中,尽管可以将其写入内部存储并在Web视图中显示,则Base64对其进行编码并将其直接加载到image标签中可能是最好的选择.

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

这篇关于如何将存储在字节数组中的图像加载到WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆