webview载入html缓慢,在Android中加载本地html页面的Webview很慢

本文介绍了一种使用Android的AssetManager加载本地HTML文件到WebView的方法,并展示了如何设置WebView以正确显示这些内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vipin..

7

尝试这段代码以获得更好的性能

AssetManager mgr = getContext().getAssets();

try {

InputStream in = mgr.open(FileName,AssetManager.ACCESS_BUFFER);

String sHTML = streamToString(in);

in.close();

//display this html in the browser

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

w.getSettings().setDefaultZoom(ZoomDensity.FAR);

w.loadDataWithBaseURL("file:///android_asset/", sHTML, "text/html", "utf-8", null);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

public static String StreamToString(InputStream in) throws IOException {

if(in == null) {

return "";

}

Writer writer = new StringWriter();

char[] buffer = new char[1024];

try {

Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

int n;

while ((n = reader.read(buffer)) != -1) {

writer.write(buffer, 0, n);

}

} finally {

}

return writer.toString();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值