Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

<html><head><metacharset="utf-8"><title>Zzzz</title><scriptsrc="../scripts/index.js"></script><linktype="text/css"href="../css/index.css">

Solution 2:

Try to use the below code for loading the html

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

instead of

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

Solution 3:

I think, It'd better use "raw" folder. That code works properly.

 InputStream is = getResources().openRawResource(R.raw.html_file);

     try {
         int size = is.available();
         byte[] buffer = newbyte[size];
         is.read(buffer);
         is.close();
         String str = new String(buffer);
         }catch (IOException e){
            e.printStackTrace();
         }
         webView.loadDataWithBaseURL("", str, "text/html", "UTF-8", "");

Solution 4:

This code it working

publicclassWebActivityextendsActivity {
  WebView wv;

 String url="file:///android_asset/sample.html";

 @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);
    wv=(WebView)findViewById(R.id.webUrl_WEB);



WebSettings webSettings = wv.getSettings();
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    wv.getSettings().setBuiltInZoomControls(true);
    wv.getSettings().setPluginState(PluginState.ON);


    wv.setWebViewClient(newmyWebClient());

    wv.loadUrl(url);
}




publicclassmyWebClientextendsWebViewClient {
    @OverridepublicvoidonPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stubsuper.onPageStarted(view, url, favicon);
    }

    @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        returntrue;

    }
}

You need to have permissions in AndroidMainfest.xml file that has access to the internet:

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

You need the sample.html file placed under the asset folder

Solution 5:

Download source code from here (Open html file from assets android)

MainActivity.java

package com.deepshikha.htmlfromassets;
 import android.app.ProgressDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;

publicclassMainActivityextendsAppCompatActivity {

WebView webview;
 ProgressDialog progressDialog;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 init();
 }

privatevoidinit(){
 webview = (WebView)findViewById(R.id.webview);
 webview.loadUrl("file:///android_asset/download.html");
 webview.requestFocus();

progressDialog = newProgressDialog(MainActivity.this);
 progressDialog.setMessage("Loading");
 progressDialog.setCancelable(false);
 progressDialog.show();

webview.setWebViewClient(newWebViewClient() {

publicvoidonPageFinished(WebView view, String url) {
 try {
 progressDialog.dismiss();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 });
 }
 }

Thanks!

Post a Comment for "Open Html File From Assets Folder"