Open HTML file from assets folder

后端 未结 5 1693
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 02:21

I am trying to open local html file in my Android application.

The file is located under my assets folder. So I am setting a WebViewClient and loading my page into i

5条回答
  •  离开以前
    2021-01-20 02:57

    If your structure should be like this:

    /assets/html/index.html

    /assets/scripts/index.js

    /assets/css/index.css

    Then just do ( Android WebView: handling orientation changes )

        if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more
            webView.loadUrl("file:///android_asset/html/index.html");
        } else {
            webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
        }
    

    Make sure you add

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setAllowUniversalAccessFromFileURLs(true);
        }
    

    Then just use urls

    
    
        
        Zzzz
        
        
    

提交回复
热议问题