问题
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.
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.
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.
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.
EDIT: My code is as follows:
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
<?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 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.
回答1:
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>
回答2:
For anyone having the same issue in the future, I solved it by adding one line to the java file. I was missing something really obvious, indeed. I also added a code part to determine back button's behaviour.
MainActivity.java is now as follows:
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");
webz.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed() {
WebView webz = (WebView) findViewById(R.id.web1);
if (webz.canGoBack()) {
webz.goBack();
} else {
super.onBackPressed();
}
}
}
来源:https://stackoverflow.com/questions/50092527/how-to-link-html-files-in-webview