可以将base64编码的imagesata直接嵌入到< img> -tag中:
这里有一个创建< img> -tag的例子(我使用一个字节数组而不是原始数据的String,因为在我的测试中,一个字符串作为源不起作用 – 我假设String不能处理二进制数 – 数据):
byte[] imageRaw = yourImage;
String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT);
String pageData = "";
Base64类引入了API v.2.2 – 对于较旧的API版本,您可以复制sourcefile from git并将其集成到您的应用程序中。它应该适用于较旧的API版本。
或者您可以使用其他类别的base64编码,如Base64Coder。
这里是检索,转换和显示图像的完整工作代码:
byte[] imageRaw = null;
try {
URL url = new URL("http://some.domain.tld/somePicture.jpg");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
out.flush();
imageRaw = out.toByteArray();
urlConnection.disconnect();
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT);
String urlStr = "http://example.com/my.jpg";
String mimeType = "text/html";
String encoding = null;
String pageData = "";
WebView wv;
wv = (WebView) findViewById(R.id.webview);
wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);