hmahdavi
hmahdavi

Reputation: 2354

How to show local html file into webview?

I want show my local html file into webview .I use this codes :

WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    wv=new WebView(this);
    setContentView(wv);

    try {
        InputStream stream = this.getAssets().open("index.html");
        int sreamsize = stream.available();
        byte[] bufer = new byte[sreamsize];
        stream.read(bufer);
        stream.close();
        String html= new String(bufer);
        wv.loadData(html, "text/html", "UTF-8");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Now I can see elements into webview with out css and js effect . How to solve this?

Upvotes: 0

Views: 372

Answers (4)

Gunaseelan
Gunaseelan

Reputation: 15515

Try to replace

try {
    InputStream stream = this.getAssets().open("index.html");
    int sreamsize = stream.available();
    byte[] bufer = new byte[sreamsize];
    stream.read(bufer);
    stream.close();
    String html= new String(bufer);
    wv.loadData(html, "text/html", "UTF-8");

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

with

wv.loadUrl("file:///android_asset/index.html");

And try again.

Update

For JS get works perfectly try

WebSettings webViewSettings = wv.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setDomStorageEnabled(true);

As @Sabari preferred.

Upvotes: 2

Sabari
Sabari

Reputation: 1981

Try this:-

WebView wv;  
  wv = (WebView) findViewById(R.id.webView1);  
       WebSettings webViewSettings = wv.getSettings();
       webViewSettings.setJavaScriptEnabled(true);
       webViewSettings.setDomStorageEnabled(true);            
       wv.loadUrl("file:///android_asset/index.html");

Upvotes: 3

User6006
User6006

Reputation: 607

Try this:-

  WebView wv;  
  wv = (WebView) findViewById(R.id.webView1);  
  wv.loadUrl("file:///android_asset/index.html"); 

Upvotes: 2

flipsandbricks
flipsandbricks

Reputation: 44

Have you tried just doing wv.loadUrl("file:///android_asset/index.html");?

Upvotes: 1

Related Questions