Webview 网页不可用 ERR_FILE_NOT_FOUND
Webview Webpage not available ERR_FILE_NOT_FOUND
我一直在使用 Kotlin 开发 android 应用程序。最近一直在尝试借助android中的Webview使用Javascript实现视频通话,问题是在加载Webview所在的activity时,没有显示 HTML 页面。
据我所知,一切就绪,我研究过的代码应该没问题。我已经重建了应用程序,但它仍然无法正常工作。
下面的代码框显示了加载页面的假定正确方法,但我只得到:“ERR_FILE_NOT_FOUND”
val filePath = "file:///android_asset/Content/call.html"
webView.loadUrl(filePath)
然后我尝试使用以下文件路径语法访问该页面:
val filePath = "./src/main/assets/Content/call.html"
它起作用了,它不再显示错误,但这是个问题,因为它在物理设备上无法正常工作。
在logcat中也显示了这个错误:
E/AndroidProtocolHandler: Unable to open asset URL: file:///android_asset/Content/call.html
我也试过将文件移出 Content 文件夹,但没有用。
这也许是一个非常简单的错误,但我找不到解决办法。如果有人能帮助我,我将不胜感激。
您应该使用 WebViewAssetLoader。这是 android 推荐的加载静态网页的方式,您不应使用文件路径。
WebviewAssetLoader 将在以下路径中托管文件 - http(s)://appassets.androidplatform.net/assets/...
在你的情况下会是 - https://appassets.androidplatform.net/assets/Content/call.html
示例代码:
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
.addPathHandler("/assets/", new AssetsPathHandler(this))
.build();
webView.setWebViewClient(new WebViewClient() {
@Override
@RequiresApi(21)
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(request.getUrl());
}
@Override
@SuppressWarnings("deprecation") // for API < 21
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(Uri.parse(request));
}
});
WebSettings webViewSettings = webView.getSettings();
// Setting this off for security. Off by default for SDK versions >= 16.
webViewSettings.setAllowFileAccessFromFileURLs(false);
// Off by default, deprecated for SDK versions >= 30.
webViewSettings.setAllowUniversalAccessFromFileURLs(false);
// Keeping these off is less critical but still a good idea, especially if your app is not
// using file:// or content:// URLs.
webViewSettings.setAllowFileAccess(false);
webViewSettings.setAllowContentAccess(false);
// Assets are hosted under http(s)://appassets.androidplatform.net/assets/... .
// If the application's assets are in the "main/assets" folder this will read the file
// from "main/assets/www/index.html" and load it as if it were hosted on:
// https://appassets.androidplatform.net/assets/www/index.html
webview.loadUrl("https://appassets.androidplatform.net/assets/www/index.html");
我刚刚解决了我的具体问题。
事实证明,我的“assets”文件夹不在“main”文件夹中,而是在“src”中的另一个文件夹中。我刚刚在“main”中创建了一个新的资产文件夹并将文件放在那里,现在 HTML 页面正确加载。必须进入项目视图。
我一直在使用 Kotlin 开发 android 应用程序。最近一直在尝试借助android中的Webview使用Javascript实现视频通话,问题是在加载Webview所在的activity时,没有显示 HTML 页面。 据我所知,一切就绪,我研究过的代码应该没问题。我已经重建了应用程序,但它仍然无法正常工作。
下面的代码框显示了加载页面的假定正确方法,但我只得到:“ERR_FILE_NOT_FOUND”
val filePath = "file:///android_asset/Content/call.html"
webView.loadUrl(filePath)
然后我尝试使用以下文件路径语法访问该页面:
val filePath = "./src/main/assets/Content/call.html"
它起作用了,它不再显示错误,但这是个问题,因为它在物理设备上无法正常工作。
在logcat中也显示了这个错误:
E/AndroidProtocolHandler: Unable to open asset URL: file:///android_asset/Content/call.html
我也试过将文件移出 Content 文件夹,但没有用。
这也许是一个非常简单的错误,但我找不到解决办法。如果有人能帮助我,我将不胜感激。
您应该使用 WebViewAssetLoader。这是 android 推荐的加载静态网页的方式,您不应使用文件路径。
WebviewAssetLoader 将在以下路径中托管文件 - http(s)://appassets.androidplatform.net/assets/...
在你的情况下会是 - https://appassets.androidplatform.net/assets/Content/call.html
示例代码:
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
.addPathHandler("/assets/", new AssetsPathHandler(this))
.build();
webView.setWebViewClient(new WebViewClient() {
@Override
@RequiresApi(21)
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(request.getUrl());
}
@Override
@SuppressWarnings("deprecation") // for API < 21
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(Uri.parse(request));
}
});
WebSettings webViewSettings = webView.getSettings();
// Setting this off for security. Off by default for SDK versions >= 16.
webViewSettings.setAllowFileAccessFromFileURLs(false);
// Off by default, deprecated for SDK versions >= 30.
webViewSettings.setAllowUniversalAccessFromFileURLs(false);
// Keeping these off is less critical but still a good idea, especially if your app is not
// using file:// or content:// URLs.
webViewSettings.setAllowFileAccess(false);
webViewSettings.setAllowContentAccess(false);
// Assets are hosted under http(s)://appassets.androidplatform.net/assets/... .
// If the application's assets are in the "main/assets" folder this will read the file
// from "main/assets/www/index.html" and load it as if it were hosted on:
// https://appassets.androidplatform.net/assets/www/index.html
webview.loadUrl("https://appassets.androidplatform.net/assets/www/index.html");
我刚刚解决了我的具体问题。 事实证明,我的“assets”文件夹不在“main”文件夹中,而是在“src”中的另一个文件夹中。我刚刚在“main”中创建了一个新的资产文件夹并将文件放在那里,现在 HTML 页面正确加载。必须进入项目视图。