Open the html file located inside my application using WebView

I just started developing applications for Android. Here I have a simple activity that contains a WebView. It works great when I try to open a website (e.g. http://www.google.com ). But I want to open the html file (suppose index.html) that I created in my res directory. The full path is res / base / index.html. Then, how to open this file in my WebView.?

Job:

webview.loadUrl("http://www.google.com"); 

Does not work:

 webview.loadUrl("./res/base/index.html"); 

Please help me.:)

+7
source share
4 answers

Put the file in the /assets directory and use:

 webview.loadUrl("file:///android_asset/index.html"); 
+10
source

First you copy the index.html file to the resource folder in your project, and then try this. It can help you.

 public class WebViewDemoActivity extends Activity { private WebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web_view_layout); mWebView = (WebView) findViewById(R.id.web_view_id); mWebView.loadUrl("file:///android_asset/index.html"); } } 
+6
source

Try something like

 webView.loadUrl("file:///android_asset/filename.html"); 
+5
source

You do not need to place the file in the resource folder. You can simply add "file:///" as the prefix of your file path.

+1
source

All Articles