问题原因:由于4.4浏览器不兼容,因为这里列出了二个样式来适应不同的浏览器版本
第一种加载:mWebView.loadUrl("file:///android_asset/chart_top01.html");
这种加载方式,会出现问题,那么采用第二种加载:
说明:这种方式是字符串的加载形式,直接给文件的地址会出现错误,因此需要把上面本地文件读出来,然后采用下面的加载方式:
webView.loadDataWithBaseURL("about:blank",bodyBuffer.toString(),"text/html", "utf-8",null);
读取assets文件的方法:这里可以读取HTML文件:
/**
* 资产文件帮助类
*/
public class AssetsUtil {
// 从assets 文件夹中获取文件并读取数据
public static String getFromAssets(Context context, String fileName) {
String result = "";
try {
InputStream in = context.getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
将上面读取的字符串二次加载:
这样能完成网页的二次添加样式,从而去掉4.4的BUG
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
WebViewUtils.setting(mWebView);
// mWebView.loadUrl("file:///android_asset/chart_top01.html");
// mWebView.loadDataWithBaseURL("about:blank",
// "APK'>file:///android_asset/chart_top01.html", "text/html",
// "utf-8",null);
// String data =
// "<HTML>在模拟器 2.1 上测试,这是<IMG src=\"APK'>file:///android_asset/igg.jpg\"/>APK里的图片";
// webView.loadDataWithBaseURL("about:blank",bodyBuffer.toString(),
// "text/html", "utf-8",null);
String data = getFromAssets("chart_top01.html");
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Log.i("fuck", currentapiVersion + "");
if (currentapiVersion >= 19) {
data += "<link rel=\"stylesheet\" type=\"text/css\" href=\"product_survey.css\" />";
mWebView.loadDataWithBaseURL("file:///android_asset/product_survey.css", data, "text/html", "utf-8", null);
} else {
data += "<link rel=\"stylesheet\" type=\"text/css\" href=\"product_survey2.css\" />";
mWebView.loadDataWithBaseURL("file:///android_asset/product_survey2.css", data, "text/html", "utf-8", null);
}
}
Webview.loadDataWithBaseURL
最新推荐文章于 2024-04-24 16:41:00 发布