【问题标题】:Can I open local html links inside a webview?我可以在 webview 中打开本地 html 链接吗?
【发布时间】:2019-09-27 15:44:56
【问题描述】:

我设置了一个可以运行的 webview。

它从名为 index.html 的 assets 文件夹的根目录加载一个页面

我希望它是 index.html 中的 html 链接,以便能够打开可以在 assets\catalyst\index.html 中找到的本地链接

我已经在根索引文件中添加了链接,但是当我点击应用程序中的链接时,它崩溃了。

我试过了

<a href="file:///android_asset/catalyst/index.html">

<a href="./catalyst/index.html">

但都不能阻止它崩溃。

我认为这是某种权限。

我在原始 webview 上设置了很多“getSettings”,希望能覆盖所有权限,但无济于事。

这是根 index.html 中的链接

<a href="file:///android_asset/catalyst/index.html"><img src="./homepage/catalystimage.jpg" alt="catalystimage"></a>

这是在允许第一个 webview 的 loadurl 的 Java 文件中。

        webView=findViewById(R.id.webviewid);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/index.html");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        WebView.setOverScrollMode(View.OVER_SCROLL_NEVER);````

i want it to load the local index.html file from the subfolder.

it crashes the app.

【问题讨论】:

  • 启用 webview 的首次调试。打开您的第一个 HTML 文件。打开你的桌面浏览器 -> 开发者工具 -> 远程设备并选择你的安卓设备。现在您可以单击您的 href 链接并查看控制台选项卡以查看抛出的错误。

标签: android html hyperlink android-webview local


【解决方案1】:

html 文件应放在assets 文件夹中(注意拼写),该文件夹将属于您项目的根目录。

所以移动

src/main/assests/index.html

assets/index.html 在 Android Studio 项目中使用此文件夹:

/app/src/main/assets/index.html

添加这一行, webView.setWebViewClient(new WebViewClient());

检查一下: Load local html file in webview android

【讨论】:

  • 嗨赛,感谢您抽出宝贵时间回复。 index.html 位于 /app/src/main/assets/ 中,可以正常加载。只是当我单击该索引文件中的 html 链接以打开 /app/src/main/assets/catalyst/index.html 下方位置的链接时,它会使应用程序崩溃。
  • 试试这个,webView.setWebViewClient(new WebViewClient());
  • 我已经有了 webview.setWebChromeclient 来启用 openGL 元素。您的建议会引起冲突吗?
猜你喜欢
  • 2011-08-13
  • 2011-01-06
  • 2018-07-20
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多