Loading Existing .HTML File with Android Webview

Loading existing .html file with android WebView

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.

Loading local html file in webView android

Try this, adding in a file:/// and doing it a little differently:

WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///data/data/com.example.example/files/file.html");

Instead of this, however, you could just put the file into your assets folder in the source code, and then do this:

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

Loading html file to webview on android from assets folder using Android Studio

The directory name should be assets not android_assets

Do like this: Sample Image

As shown in the above pics just right click on your app->New->Folder->Assets Folder

Now put your .html file here in assets folder.

That's it. Done.

Remaining is same in code what you did.

WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/hello.html");
setContentView(view);

Loading html file from local folder into webview

You can use:


WebView webView = // ...

webView.loadUrl("file:///myPath/myFile.html");

In an Android application, files can be read from 3 types of locations:

  • Internal storage: Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html

  • External storage: Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"

  • Assets: Stored in the apk. Read-only access. URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView)

Load an external html file into webview

Don't create file:// URLs yourself, as you will tend to screw them up. In this case, I think that you have four slashes after the :, three that you typed in and one from Environment.getExternalStorageDirectory().

Instead, create a File object and use that as the basis:

File f = new File(Environment.getExternalStorageDirectory(), "Android/data/com.example/files/test_html2.html");
webview.loadUrl(f.toURI().toURL()); // or use Uri.fromFile(f).toString() instead


Related Topics



Leave a reply



Submit