当前位置: 代码迷 >> Web前端 >> 关于WebView的loadData步骤(有乱码和不能显示图片等)
  详细解决方案

关于WebView的loadData步骤(有乱码和不能显示图片等)

热度:426   发布时间:2012-06-26 10:04:14.0
关于WebView的loadData方法(有乱码和不能显示图片等)

通过以下转来的文章知道:

?

1、在使用webview.loadData(String data, String mimeType, String encoding)方法时,可能有乱码,

解决方法是在此方法之前调用下面方法:

webview.getSettings().setDefaultTextEncodingName("utf-8") ;


2、loadData方法不能显示图片的问题请参考下面文章。

?

转来的文章:

?

WebViewAndroid应用开发中常用的组件,我们可以通过它来显示网页或者html格式的String数据.

对于后者我们往往使用loadData方法来加载html数据。下面就笔者的实践来讨论一下WebViewloadData方法。

????对于loadData方法,ADK中的介绍为Load the given data into the WebView. This will load the data into WebView using the data: scheme. Content loaded through this mechanism does not have the ability to load content from the network.即,loadData主要被设计用来装载URI格式的数据,它不能通过网络来加载内容。使用的过程中笔者遇到并且解决了两个问题。

1.??????经过实践,笔者发现,loadData不能加载图片内容,如果要加载图片内容或者获得更强大的Web支持请使用loadDataWithBaseURL

2.??????许多实用loadData方法的朋友都遇到显示乱码的问题,那是因为编码器设置错误导致的。我们知道String类型的数据主要是unicode编码,而WebView一般为了节省资源使用的是UTF-8编码,所以我们在loadData的时候要告诉方法怎样转码。即要告诉它要将unicode编码的内容转成UTF-8编码的内容。有些朋友虽然在loadData的时候设置了编码方式,但是还是显示乱码,这是因为还需要为WebViewtext编码指定编码方式。举例如下:

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

??????????????????????String content = getUnicodeContent() ;

???????????????wv.getSettings().setDefaultTextEncodingName(“UTF?-8”) ;

???????????????wv.loadData(content, “text/html”, “UTF-8”) ;

文章来源:

http://blog.sina.com.cn/s/blog_4c0706560100r8qy.html