如何在WebView中链接html文件?

问题描述 投票:0回答:2

我有一个巨大的htm文件夹和一个html文件,其中包含每个htm文件的href链接。我想创建一个Android应用程序,供个人使用,以便在平板电脑上更轻松地浏览这些文件。

我设法将主html文件加载到Android Studio中的WebView,但是单击任何URL会导致崩溃,因此我无法通过我的html目录加载任何htm文件。

我在android编程方面没有经验,我从网络创建方面只有一些过时的经验,从那些带有一些css的html就足够了,所以我试着用我的直觉。我可能会遗漏一些非常明显的东西,但我无法解决我的问题。

也许你能帮助我?是否可以通过WebView中的href在html文件之间重定向?或者也许有一种简单的方法可以绕过它?遗憾的是,从头编写目录是不可能的 - 有数千个htm文件。欢迎任何建议。

编辑:我的代码如下:

main activity.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只是body部分中一个href链接的巨大列表。它正确加载到WebView,即使css文件中的简单格式化也是正确的,但单击链接会导致崩溃。有成千上万的这样的行:

<a href="view-1.htm" title="file description">file title</a>

我也尝试过这种方式改变它(这会成千上万行有问题......)但它也不起作用:

<a href="file:///android_asset/view-1.htm" title="file description">file title</a>

所有文件都在assets文件夹中。

android html android-studio webview
2个回答
0
投票

您需要设置StrictMode策略以公开URI(在您的情况下为view-1.html)。您可以按如下方式获得所需的结果:

main activity.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>

0
投票

对于将来有同样问题的人,我通过在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");
        webz.setWebViewClient(new WebViewClient());

    }

    @Override
    public void onBackPressed() {
        WebView webz = (WebView) findViewById(R.id.web1);
        if (webz.canGoBack()) {
            webz.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.