如何在WebView中链接html文件?
我有一个很大的htm文件文件夹和一个HTML文件,其中的href链接到每个htm文件.我想创建一个供个人使用的android应用程序,以使在平板电脑上浏览这些文件更加容易.
I have a huge folder of htm files and a single html file with a href links to each of these htm files. I would like to create an android app, for personal use, to make browsing these files easier on a tablet.
我设法将主要html文件加载到Android Studio中的WebView,但是单击任何URL都会导致崩溃,因此我无法通过html目录加载任何htm文件.
I managed to load the main html file to WebView in Android Studio, but clicking any URL results in a crash, therefore I can't load any htm file through my html catalog.
我在android编程方面没有经验,从html和一些css就足够了,所以我在网站创建方面只有一些过时的经验,所以我尝试运用自己的直觉.我可能会遗漏一些非常明显的东西,但是我没有设法解决我的问题.
I am not experienced in android programming, I only have some out-of-date experience in website creation back from the days when html with some css was enough, so I try to use my intuition. I might be missing something very obvious, but I didn't manage to solve my issue.
也许您可以为我提供帮助?是否可以通过WebView中的href在html文件之间重定向?还是有一种简单的方法可以绕过它?不幸的是,从头开始编码目录是毫无疑问的-有成千上万个htm文件.欢迎任何建议.
Maybe you'll be able to help me? Is it possible to redirect between html files via a href in WebView? Or maybe there is a simple way to bypass it? Coding the catalog from scratch is unfortunately out of question - there are thousands of htm files. Any suggestions are welcome.
我的代码如下:
MainActivity.java
MainActivity.java
package a.my.app4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import java.nio.channels.WritableByteChannel;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webz=(WebView)findViewById(R.id.web1);
webz.loadUrl("file:///android_asset/index.html");
}
}
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/web1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp" />
</RelativeLayout>
index.html只是正文部分中href链接的巨大列表.它已正确加载到WebView,即使来自css文件的简单格式也是正确的,但是单击链接会导致崩溃.有成千上万的这样的行:
index.html is just a huge list of a href links in the body section. It is loaded correctly to the WebView, even the simple formatting from css files is correct, but clicking the links leads to a crash. There are thousands of lines like that:
<a href="view-1.htm" title="file description">file title</a>
我也尝试过以这种方式更改它(这可能会影响成千上万行...),但是它也不起作用:
I also tried changing it that way (which would be problematic with thousands of lines...) but it didn't work either:
<a href="file:///android_asset/view-1.htm" title="file description">file title</a>
所有文件都在资产文件夹中.
All the files are in the assets folder.
您需要设置StrictMode策略才能公开URI(在您的情况下为 view-1.html
).您可以达到所需的结果,如下所示:
You need to set the StrictMode policy in order to expose the URI (in your case view-1.html
). You can achieve your desired result as follows:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
WebView webView = findViewById(R.id.wvWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/index.html");
}
index.html
<html>
<body>
<H1>Hello</H1>
<a href="view1.html" title="file description">file title</a>
</body>
</html>
view1.html
<html>
<body>
<H1>Hello View 1</H1>
</body>
</html>