Search code examples
androidwebviewassets

Open HTML file from assets folder


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 it. But I get a "webpage not available" error.

Here is my Activity code :

public class LocalDialogActivity extends Activity {

    protected WebView webView;
    private static final String ENROLLMENT_URL = "file:///assets/enrollment.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_dialog);

        webView = (WebView)findViewById(R.id.local_dialog_webview);
        webView.setWebViewClient(new WebViewClient());

        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);


        refreshWebView(webView);

    }

    public void refreshWebView(View view) {
        webView.loadUrl(ENROLLMENT_URL);
    }

and the layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView
        android:id="@+id/local_dialog_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

And i have internet permission in my AndroidMainfest.xml file that has access to the internet:

<uses-permission android:name="android.permission.INTERNET" />

Any help would be welcome. Thank you.


Solution

  • Try to use the below code for loading the html

    "file:///android_asset/enrollment.html" 
    

    instead of

    "file:///assets/enrollment.html"