如何显示图像的WebView

如何显示图像的WebView

本文介绍了如何显示图像的WebView loaddata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下

String urlStr = "http://example.com/my.jpg"
String mimeType = "image/jpeg";
String encoding = null;
String pageData = ""; // This is data read in from an HttpURLConnection
webview.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);

但是当我运行它,我看到的是一个蓝色的问号,而不是我的形象。什么是正确的方式来处理与loadData一个web视图显示图像?

but when I run this, all I see is a blue question mark instead of my image. What is the proper way to handle displaying an image in a WebView with loadData?

编辑:有没有办法做到这一点没有经过pageData为< IMG SRC =htt​​p://example.com/my.jpg/> ?看来愚蠢的loadData需要一个MIME类型,如果它只能处理text / html的。特别是因为javadoc的名单为image / jpeg为例MIME类型,你可能会传递

Is there a way to do this without passing pageData as <img src="http://example.com/my.jpg/"> ? It seems silly that loadData takes a mime-type if it can only handle "text/html". Especially since the javadoc lists "image/jpeg" as an example mime-type that you might pass in.

推荐答案

它可以使嵌入中的Base64 EN codeD的ImageData直接进入&LT; IMG&GT; -tag:

It is possible to embedd the base64 encoded imagedata direct into the <img>-tag:

下面创建一个例子&LT; IMG&GT; -tag(我使用一个字节数组,而不是字符串的原始数据,因为在我的测试中一个String作为源没有工作 - 我认为字符串不能处理的二进制数据):

Here an example for creating the <img>-tag (I use an byte-array instead the String for raw-data, because in my tests an String as source didn't work - I assume that String can't handle binary-data):

  byte[] imageRaw = yourImage;
  String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT);
  String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />";

中的Base64类中引入了API V.2.2 - 老年API的版本,你可以复制的<一个href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/java/android/util/Base64.java;hb=HEAD">sourcefile从混帐 并在应用程序中整合它。它应该与旧API的版本。

The Base64-class was introduced with API v.2.2 - for older API-versions you can copy the sourcefile from git and integrate it in your app. It should work with older API-versions.

或者你可以使用的替代类的base64编码一个像 Base64的codeR

Or you can use one of the alternative classes for base64-encoding like Base64Coder.

和这里的整个工作code:获取,转换和显示图像:

And here the complete working code for retrieving, converting and showing the image:

  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 = "<img src=\"data:image/jpeg;base64," + image64 + "\" />";

  WebView wv;
  wv = (WebView) findViewById(R.id.webview);
  wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);

这篇关于如何显示图像的WebView loaddata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:44