android - 我可以在 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.
解决方案
html 文件应放在assets
文件夹中(注意拼写),该文件夹将属于您项目的根目录。
所以动起来
src/main/assests/index.html
至
assets/index.html
在 Android Studio 项目中使用此文件夹:
/app/src/main/assets/index.html
添加这一行,
webView.setWebViewClient(new WebViewClient());
推荐阅读
- swift - 快速 NSPredicate 数据(结构)
- php - Doctrine LockMode::PESSIMISTIC 数据库重复
- jquery-ui - 自动完成:在悬停和按下向上和向下键时更改颜色
- python - 我想在某些条件下从列表 python 列表中创建一个列表,我提到了预期的输出
- python-3.x - “SalesforceBulk”对象没有属性“post_bulk_batch”
- xml - 将结构化 XML 国际邮寄地址格式化为文本
- c# - CSharpScript - TypeLoadException
- wordpress - 无法修复主页中的空间问题
- android - 如何在android中使用改造将参数和值传递给发布api?
- java - JOOQ 使用转换器将字符串转换为枚举