【发布时间】:2020-11-17 13:50:28
【问题描述】:
我的应用通过WebView#loadUrl()加载位于getFilesDir()下的本地html文件。
在targetSdkVersion = 29 之前,下面的代码正在运行。
copyAssetsFile(this, "sample.html", getFilesDir().getAbsolutePath());
webView.getSettings().setJavaScriptEnabled(true);
String url = "file://" + getFilesDir().getAbsolutePath() + "/sample.html";
webView.loadUrl(url);
}
private static void copyAssetsFile(Context context, String fileName, String directoryPath) {
try {
InputStream inputStream = context.getResources().getAssets().open(fileName);
FileOutputStream fileOutputStream = new FileOutputStream(
new File(directoryPath, fileName), false);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) >= 0) {
fileOutputStream.write(buffer, 0, length);
}
fileOutputStream.close();
inputStream.close();
完整的例子是here。
但是,更改 targetSdkVersion = 30 后它不起作用。
- WebView 回复
net::ERR_ACCESS_DINIED - 如果本地 html 位于
android_asset,则可以加载它
如何在targetSdkVersion = 30上加载本地html文件?
是不是改成被Android FW拒绝了??
【问题讨论】:
-
我们不相信你的代码,因为 webview 不会开始谈论资产。
标签: android android-webview android-11