Problems loading html asset into webview

2019-01-20 04:38发布

问题:

I am having difficulty in loading a html file from my project assets folder into a webview. I have looked at dozens of tutorials and solutions but none seem to work for me.

In my project's assets folder I have two simple html files. index.html and faq.html (The plan is to utilise this structure for my help documentation)

My code:

    WebView wv = (WebView)findViewById(R.id.webview1);
    wv.setWebViewClient(new WebViewClient() {  
          @Override  
          public boolean shouldOverrideUrlLoading(WebView view, String url)  
          {  
            view.loadUrl(url);
            return true;
          }  
        });         
    wv.loadUrl("file:///android_asset/index.html");    

The webview displays the following:

Web Page Not Available

The Web Page at file:///android_asset/index.html could not be loaded as:

The requested file was not found. index.html

From everything I have read what I have here should work, but it does not.

回答1:

your usage is right, so if has this problem, you need check the index.html file existed or not carefully, also you can clean the project, and rebuild it.



回答2:

You can try this code ....

WebView myBrowser;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String myURL = "file:///android_asset/index.html";
        myBrowser=(WebView)findViewById(R.id.mybrowser);

        /*By default Javascript is turned off,
         * it can be enabled by this line.
         */
        myBrowser.getSettings().setJavaScriptEnabled(true);
        myBrowser.setWebViewClient(new WebViewClient());

        myBrowser.loadUrl(myURL);

    }