首页 > 解决方案 > 我可以在 webview 中打开本地 html 链接吗?

问题描述

我有一个有效的 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.

标签: androidhtmlhyperlinkandroid-webviewlocal

解决方案


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

所以动起来

src/main/assests/index.html

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

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

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

检查它: 在webview android中加载本地html文件


推荐阅读