赞
踩
Harmony鸿蒙webview打开本地网页,dataability方法
一,在config.json文件中增加配置
"abilities": [
{
"name": "包名.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "$string:entry_MainAbility",
"type": "page",
"launchType": "standard"
},
{
"name": "包名.DataAbility",
"type": "data",
"uri": "dataability://包名.DataAbility"
}
]
二,新建DataAbility
public class DataAbility extends Ability { @Override public RawFileDescriptor openRawFile(Uri uri, String mode) throws FileNotFoundException { if (uri == null) {; return super.openRawFile(uri, mode); } String path = uri.getEncodedPath(); int splitIndex = path.indexOf('/', 1); String providerName = Uri.decode(path.substring(1, splitIndex)); String rawFilePath = Uri.decode(path.substring(splitIndex + 1)); RawFileDescriptor rawFileDescriptor = null; try { rawFileDescriptor = getResourceManager().getRawFileEntry(rawFilePath).openRawFileDescriptor(); } catch (IOException e) { // 异常处理 } return rawFileDescriptor; } }
三、调用
private static final String URL_LOCAL = "dataability://包名.DataAbility/resources/rawfile/根目录/index.html";
//把你的网页放到根目录下,根目录这三个字可以改为你自己的名称
webView.getWebConfig().setDataAbilityPermit(true);//这个要加上,设置webview支持打开本地文件
webView.load(URL_LOCAL);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。