从当前HTML文件Android Studio中打开本地HTML文件?

时间:2018-08-18 09:55:55

标签: android html android-studio android-webview

我在Android Studio上制作了一个WebView应用。 它会正确打开我的默认索引HTML页面

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_kurven_untersucher);
    WebView browser = (WebView)findViewById(R.id.browser);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.loadUrl("file:///android_asset/www/index.html");

    mVisible = true;
    mControlsView = findViewById(R.id.fullscreen_content_controls);
    mContentView = findViewById(R.id.fullscreen_content);


    // Set up the user interaction to manually show or hide the system UI.
    mContentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            toggle();
        }
    });

    // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
}

到现在为止还不错。现在,我在HTML页面中创建了一个链接,以在本地目录中打开另一个HTML文件

<!DOCTYPE html>
<html lang="de">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>app</title>
</head>
<body>
    <a href="test.html">test</a>
</body>

但是当我在Android手机上安装APK时,点击该链接后它就会崩溃。

我该如何解决?

这是logcat错误

08-18 12:51:22.798 22903-22903/com.thejami.kurvenuntersucher E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thejami.kurvenuntersucher, PID: 22903
android.os.FileUriExposedException: file:///android_asset/www/test.html exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
    at android.net.Uri.checkFileUriExposed(Uri.java:2356)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10511)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10465)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
    at android.app.Activity.startActivityForResult(Activity.java:4564)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:757)
    at android.app.Activity.startActivityForResult(Activity.java:4522)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:744)
    at android.app.Activity.startActivity(Activity.java:4883)
    at android.app.Activity.startActivity(Activity.java:4851)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:377)
    at ahj.startActivity(SourceFile:22)
    at aeV.a(SourceFile:28)
    at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(SourceFile:173)
    at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
    at org.chromium.base.SystemMessageHandler.handleMessage(SourceFile:9)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

4 个答案:

答案 0 :(得分:0)

您的Web视图将不会显示文件test.html。取而代之的是,webview创建了一个意图,让用户选择外部浏览器。然后,您将获得FileUriExposedException。

如果要在Webview本身中加载文件,则应让schouldOverrideUrlLoading返回true或false。

或者类似的东西。

答案 1 :(得分:0)

尝试:

 WebView lWebView = (WebView)findViewById(R.id.webView);
 File lFile = new File(Environment.getExternalStorageDirectory() + " 
 <FOLDER_PATH_TO_FILE>/<FILE_NAME>");
 lWebView.loadUrl("file:///" + lFile.getAbsolutePath());

答案 2 :(得分:0)

您可以从以下代码中使用:

  webView.setWebViewClient(new WebViewClient());

答案 3 :(得分:0)

只需将其添加到webView代码下方,即可解决问题

browser.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
        }
});