【问题标题】:Webview Webpage not available ERR_FILE_NOT_FOUNDWebview 网页不可用 ERR_FILE_NOT_FOUND
【发布时间】:2021-05-19 16:35:57
【问题描述】:

我一直在使用 Kotlin 开发一个 android 应用程序。最近,我一直在尝试在android中借助Webview使用Javascript实现视频通话,问题是在加载Webview所在的活动时,它没有显示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 文件夹之外,但不起作用。

这可能是一个非常简单的错误,但我找不到解决方案。如果有人能帮助我,我们将不胜感激,谢谢。

【问题讨论】:

    标签: android kotlin webview


    【解决方案1】:

    您应该使用 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");
     
    

    【讨论】:

      【解决方案2】:

      我刚刚解决了我的具体问题。 事实证明,我的“assets”文件夹不在“main”文件夹中,而是在“src”中的另一个文件夹中。我刚刚在“main”中创建了一个新的资产文件夹并将文件放在那里,现在 HTML 页面可以正确加载。必须进入项目视图。

      【讨论】:

        猜你喜欢
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 2019-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多