android loaddata代码,android – 如何使用WebView loaddata显示图像?

可以将base64编码的imagesata直接嵌入到< img> -tag中:

这里有一个创建< img> -tag的例子(我使用一个字节数组而不是原始数据的String,因为在我的测试中,一个字符串作为源不起作用 – 我假设String不能处理二进制数 – 数据):

byte[] imageRaw = yourImage;

String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT);

String pageData = "jpeg;base64,%22";

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 = "jpeg;base64,%22";

WebView wv;

wv = (WebView) findViewById(R.id.webview);

wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值