93

I did try samples, demos from Google codes and other resources with WebView, but when i try to do it in my own code, it doesn't work for me.

I want to load myfile.html which i put in assets folder, and using:

private WebView myWebView;

myWebView.loadUrl("file:///android_assets/myfile.html");

On emulator shows error

The web page at file:///android_assets/myfile.html could not be loaded as: The requested file was not found. /android_assets/myfile.html

When i put that file to res/raw/ folder and using:

myWebView.loadUrl("file:///android_res/raw/myfile.html");

then only emulator android 2.2 API level 8 can load the file probably, other older versions show the same error. Am i missing something?

Is there any way of loading an existing .html file in the application package which works on all API versions ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
laph
  • 2,925
  • 2
  • 18
  • 18

6 Answers6

171

ok, that was my very stupid mistake. I post the answer here just in case someone has the same problem.

The correct path for files stored in assets folder is file:///android_asset/* (with no "s" for assets folder which i was always thinking it must have a "s").

And, mWebView.loadUrl("file:///android_asset/myfile.html"); works under all API levels.

I still not figure out why mWebView.loadUrl("file:///android_res/raw/myfile.html"); works only on API level 8. But it doesn't matter now.

skaffman
  • 398,947
  • 96
  • 818
  • 769
laph
  • 2,925
  • 2
  • 18
  • 18
  • 54
    FWIW I don't think it was a stupid mistake. I made the same mistake now, twice. It's not intuitive! First make a folder called "assets" then refer to it by "android_asset" (no "s")?? It's the platform that's stupid, IMO :P – richtaur Jan 12 '11 at 00:49
  • 3
    to bad `raw` did not work. I would have use for `raw-de`, `raw-fr`and so on. Now I have to do it myself. – Martin Jun 27 '11 at 16:29
  • i tried the same way guided by laph but when i put my xml file in the folder mentioned by you myWebView.loadUrl("file:///android_res/raw/myfile.xml"); It gives me error that Make sure internet or path is correct and when i write myWebView.loadUrl("file://android_res/raw/myfile.xml"); it do not give any error but it also shows nothing.Please help me in this. –  Nov 16 '11 at 05:07
  • 2
    Hi Aditya, i believe that error is because your file is .xml. loadUrl suppose to load a .html file. Please correct me if i'm wrong. – laph Nov 28 '11 at 23:03
  • Is this possible? Folder assets contains Javascript and other files, folder res/raw contains HTML files – AxelS Oct 12 '16 at 01:42
  • This does not seem to work for localized resources as @martin pointed out. Does anyone have a clue where to start? I need to show 3 languages,and have the file-en.html, file-es.html and file-de.html files, all set. – coya Sep 04 '17 at 19:58
  • To use the raw folder use "android_res/raw/". More details here: https://stackoverflow.com/a/14171362/753632 – Johann Sep 29 '19 at 10:50
20

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>
    <meta charset="utf-8">
    <title>Zzzz</title>
    <script src="../scripts/index.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/index.css">
Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
17

paste your .html file in assets folder of your project folder. and create an xml file in layout folder with the fol code: my.xml:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
    />

add fol code in activity

setContentView(R.layout.my);
    WebView mWebView = null;
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("file:///android_asset/new.html"); //new.html is html file name.
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Naresh
  • 171
  • 2
6

Copy and Paste Your .html file in the assets folder of your Project and add below code in your Activity on onCreate().

        WebView view = new WebView(this);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl("file:///android_asset/**YOUR FILE NAME**.html");
        view.setBackgroundColor(Color.TRANSPARENT);
        setContentView(view);
Arunkumar
  • 224
  • 2
  • 6
2

You could read the html file manually and then use loadData or loadDataWithBaseUrl methods of WebView to show it.

Lachezar
  • 6,523
  • 3
  • 33
  • 34
  • Hi Lucho, thanks for your answer. You means that i have to convert my .html file into String, then load it with loadData or loadDataWithBaseUrl method? – laph Oct 26 '10 at 22:19
  • 2
    my .html files are quite big to convert to string quickly. Any idea to load it with absolute path? – laph Oct 26 '10 at 22:29
2

The debug compilation is different from the release one, so:

Consider your Project file structure like that [this case if for a Debug assemble]:

src
  |
  debug
      |
      assets
           |
           index.html

You should call index.html into your WebView like:

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

So forth, for the Release assemble, it should be like:

src
  |
  release
        |
        assets
             |
             index.html

The bellow structure also works, for both compilations [debug and release]:

src
  |
  main
     |
     assets
          |
          index.html
PYK
  • 3,674
  • 29
  • 17