3

I am trying to make an app that opens a local html page which has hyperlinks linked to 2 other pages. All these html files are placed in assets folder. Now when I touch the hyperlink one the first html file, the app crashes intsead of opening the other html file.

package com.example.rishabh.webviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
    WebView web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web= (WebView) findViewById(R.id.webview);
        web.loadUrl("file:///android_asset/1.html");
    }
}

Here's the log

W/System.err: android.os.FileUriExposedException:file:///android_asset/2.html exposed beyond app through Intent.getData()
W/System.err:     at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
W/System.err:     at android.net.Uri.checkFileUriExposed(Uri.java:2346)
W/System.err:     at android.content.Intent.prepareToLeaveProcess(Intent.java:8965)
W/System.err:     at android.content.Intent.prepareToLeaveProcess(Intent.java:8926)
W/System.err:     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:4225)
W/System.err:     at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
W/System.err:     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:4183)
W/System.err:     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
W/System.err:     at android.app.Activity.startActivity(Activity.java:4522)
W/System.err:     at android.app.Activity.startActivity(Activity.java:4490)
W/System.err:     at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)
W/System.err:     at org.chromium.android_webview.ResourcesContextWrapperFactory$WebViewContextWrapper.startActivity(ResourcesContextWrapperFactory.java:121)
W/System.err:     at org.chromium.android_webview.AwContentsClient.sendBrowsingIntent(AwContentsClient.java:203)
W/System.err:     at org.chromium.android_webview.AwContentsClient.shouldIgnoreNavigation(AwContentsClient.java:170)
W/System.err:     at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:256)
W/System.err:     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/System.err:     at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 A/chromium: [FATAL:jni_android.cc(236)] Please include Java exception stack in crash report
W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
W/google-breakpad: Chrome build fingerprint:
W/google-breakpad: 1.0
W/google-breakpad: 1
W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
    A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 4203 (abh.webviewdemo)

    [ 11-13 22:33:57.340  1186: 1186 W/         ]
    debuggerd: handling request: pid=4203 uid=10073 gid=10073 tid=4203
Application terminated.
Smittey
  • 2,475
  • 10
  • 28
  • 35
  • I am using the latest android studio and my emulator runs on Noughat – Rishabh Bhatia Nov 13 '16 at 17:17
  • 4
    The default behavior of `WebView` is to send clicked-upon links to the default Web browser. You should use a `WebViewClient` and `shouldOverrideUrlLoading()` to keep those links within your `WebView`. – CommonsWare Nov 13 '16 at 17:19
  • @CommonsWare What if I want to open the link in an external browser. How should I approach the problem in this scenario? – N. Park Oct 01 '18 at 14:42
  • @N.Park: That is the default behavior of `WebView`, last I checked. It should "just work". If you are running into specific problems with that, ask a separate Stack Overflow question with a [mcve]. – CommonsWare Oct 01 '18 at 21:49

1 Answers1

5

As @CommonsWare suggested, it appears that simply using the following method, solves the problem for N/25 devices trying to load asset based linked html files. Tested on an emulator running Nougat:

    web.setWebViewClient(new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            Log.i("WebView", "Attempting to load URL: " + url);

            view.loadUrl(url);
            return true;
        }
    });
behelit
  • 1,765
  • 2
  • 20
  • 34
  • Actually you can simply `return false` (and keep the back button behavior). – aghidini Feb 26 '17 at 18:22
  • I do see this crash in my Dashboard, but I cannot reproduce this problem on the emulator (maybe depends on the browser associated with the Intent to trigger HTML links?) However, with the proposed solution the links mailto: and tel: do not work. I can think of excluding this type of links from being loaded in the WebView (and also, to only use the shouldOverrideUrlLoading for APIs over 7.0). – kikoso Aug 28 '17 at 02:58