---
title: "Android使用WebView加载本地资源"
author: "sfshine"
date: "2012-11-10T10:39:00Z"
tags: []
slug: "android-webview"
draft: false
lastmod: "2012-11-13T21:12:43Z"
id: 1724923
url: "https://sfshine.iteye.com/blog/1724923"
---

#### public voidloadData([String]()data,[String]()mimeType,[String]()encoding)

则会出现html代码中图片路径不对导致不能正常显示图片（通常是一个白色的固定大小的框，或者没有）

当把路径改为绝对路径加上file://都还无法解决

终于，在网上查资料的时候发现了这个函数，正好解决这个问题

#### []()public voidloadDataWithBaseURL([String]()baseUrl,[String]()data,[String]()mimeType,[String]()encoding,[String]()historyUrl)

baseUrl是图片的目录，在html代码中就写关于此目录的相对路径就可以了，哇嘎嘎！~

附带代码解释：

String htmlPath = "file:///mnt/sdcard/test/11.html";

String baseUrl = "file:///mnt/sdcard/test/";

webView.loadDataWithBaseURL(baseUrl, data, "text/html", "utf-8", null);

则两个调用都可以显示正常的html网页了，并且前一种可以对针对不同分辨率大小的屏幕做缩放了！！

在网上看了许多关于WebView 的例子。但当自己做起来时，总是有些差别，是另一种体会。这节我就以 模拟器为2.1 为例来讲。

WebView 主要调用三个方法：LoadUrl、LoadData、LoadDataWithBaseURL。

1、LoadUrl 直接加载网页、图片并显示。（本地或是网络上的网页、图片、gif）

2、LoadData 显示文字与图片内容 （模拟器1.5、1.6）

3、LoadDataWithBase 显示文字与图片内容（支持多个模拟器版本）

下面来看看代码片段：

Java代码[![收藏代码](http://zhujianjia.iteye.com/images/icon_star.png)]()

01. packagecom.webviewdemo;
02. importjava.net.URLEncoder;
03. importandroid.app.Activity;
04. importandroid.os.Bundle;
05. importandroid.webkit.WebView;
06. publicclassWebViewDemoextendsActivity{
07. privateWebViewMyWebView;
08. staticfinalStringmimeType="text/html";
09. staticfinalStringencoding="utf-8";
10. /\*\*Calledwhentheactivityisfirstcreated.\*/
11. @Override
12. publicvoidonCreate(BundlesavedInstanceState){
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.main);
15. //
16. MyWebView=(WebView)findViewById(R.id.MyWebview);
17. //this.webHtml();
18. //this.webImage();
19. //this.localHtmlZh();
20. //this.localImage();
21. //this.localHtml();
22. this.localHtmlImage();
23. }
24. /\**
25. \*直接网页显示
26. \*/
27. privatevoidwebHtml(){
28. try{
29. MyWebView.loadUrl("http://www.google.com");
30. }catch(Exceptionex){
31. ex.printStackTrace();
32. }
33. }
34. /\**
35. \*直接网络图片显示
36. \*/
37. privatevoidwebImage(){
38. try{
39. MyWebView.loadUrl("http://www.google.com.hk/intl/zh-CN/images/logo\_cn.gif");
40. }catch(Exceptionex){
41. ex.printStackTrace();
42. }
43. }
44. /\**
45. \*中文显示
46. \*/
47. privatevoidlocalHtmlZh(){
48. try{
49. Stringdata="&lt;html&gt;在模拟器2.1上测试&lt;/html&gt;";
50. //utf-8编码处理(在SDK1.5模拟器和真实设备上都将出现乱码,SDK1.6上能正常显示)
51. //MyWebView.loadData(data,mimeType,encoding);
52. //对数据进行编码处理(SDK1.5版本)
53. MyWebView.loadData(URLEncoder.encode(data,encoding),mimeType,encoding);
54. //MyWebView.loadDataWithBaseURL(null,data,mimeType,encoding,null);
55. }catch(Exceptionex){
56. ex.printStackTrace();
57. }
58. }
59. /\**
60. \*显示本地图片文件
61. \*/
62. privatevoidlocalImage(){
63. try{
64. //本地文件处理
65. Stringstr="file:///android\_asset/icon.png";
66. MyWebView.loadUrl(str);
67. }catch(Exceptionex){
68. ex.printStackTrace();
69. }
70. }
71. /\**
72. \*显示本地网页文件
73. \*/
74. privatevoidlocalHtml(){
75. try{
76. //本地文件处理(如果文件名中有空格需要用+来替代)
77. MyWebView.loadUrl("file:///android\_asset/test.html");
78. }catch(Exceptionex){
79. ex.printStackTrace();
80. }
81. }
82. /\**
83. \*显示本地图片和文字混合的Html内容
84. \*/
85. privatevoidlocalHtmlImage(){
86. try{
87. Stringdata="&lt;HTML&gt;在模拟器2.1上测试,这是&lt;IMGsrc=\\"APK'&gt;file:///android\_asset/igg.jpg\\"/&gt;APK里的图片";
88. //SDK1.5本地文件处理(不能显示图片)
89. //MyWebView.loadData(URLEncoder.encode(data,encoding),mimeType,encoding);
90. //SDK1.6及以后版本
91. //MyWebView.loadData(data,mimeType,encoding);
92. //本地文件处理(能显示图片)
93. MyWebView.loadDataWithBaseURL(null,data,mimeType,encoding,null);
94. }catch(Exceptionex){
95. ex.printStackTrace();
96. }
97. }
98. }

运行 localHtmlImage(...) 这个方法的结果：

![](http://dl.iteye.com/upload/attachment/476648/8d6e1bb6-06f5-3ace-a6dd-f36c6d8a5eb1.jpg)

在上面的所有方法中，模拟器2.1下 调用 LoadData 方法总是出现乱码。我想应该是2.1已经不在用它了吧。直接用LoadDataWithBaseURL 方法最简单，不再牵涉编码的转换。

**记住：显示本地文件时 必须使用[APK'&gt;file:///]()作为前缀。**

转自：[http://hi.baidu.com/455611934/blog/item/027ffe5d17d8a652faf2c02c.html](http://hi.baidu.com/455611934/blog/item/027ffe5d17d8a652faf2c02c.html)