2

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.

Daniel Neilsen
  • 33
  • 1
  • 1
  • 3
  • possible duplicate of [Webview load html from assets directory](http://stackoverflow.com/questions/3152422/webview-load-html-from-assets-directory) – Dharmendra Jan 05 '12 at 04:14

2 Answers2

3

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.

idiottiger
  • 5,147
  • 2
  • 25
  • 21
  • I have done a project clean with no result. How can I test if index.html existed? Is there a way to check the contents of the apk that ha sbeen installed on the emulator? – Daniel Neilsen Jan 05 '12 at 04:27
  • 1
    the project path has a bin folder, the apk will save to here, you can use the zip tool to extra the file, you can check the asset folder. – idiottiger Jan 05 '12 at 04:31
  • Try accessing some other web page...if that loads then problem lies with index.html....there does not seem any error/problem with the code you have posted. – Usama Sarwar Jan 05 '12 at 04:41
  • I have opened the apk and neither assets/index.html or assets/faq.html are in the apk. Do I have to manually include the assets folder to be included? I thought eclipse did this automatically when setting up the project. – Daniel Neilsen Jan 05 '12 at 04:47
  • 1
    I think I have found the cause of the problem. My project comprises of the project and then a separate library project. I was placing the files in the /assets folder of the library project which was not working but when placed in the /assets folder of the main project it works fine. I just need to figure out how to include the /assets folder of the library project in the apk. Thanks for the Help :) – Daniel Neilsen Jan 05 '12 at 05:19
-1
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);

    }