0

I made a WebView app on Android Studio. It opens my default index HTML page correctly

@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.
}

till now it's good. Now I made a link in my HTML page to open another HTML file in my local directory

<!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>

but when I install the APK on my android phone, it crashes after clicking on that link.

how can I solve this?

Here is the logcat error

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)
Tareq Jami
  • 89
  • 10

4 Answers4

1

you can use from below code:

  webView.setWebViewClient(new WebViewClient());
Mehrdad
  • 1,477
  • 15
  • 17
0

Your webview is not going to display file test.html. Instead the webview creates an intent to let the user choose an external browser. Then you get a FileUriExposedException.

If you want to load the file in the webview itself you should let schouldOverrideUrlLoading return true or false.

Or something like that.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

try it :

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

Just adding this below the webView codes, solved the problem

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