Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading html file from local folder into webview

I'm new to Android development.

I want to load a html file into a webview.

Note that there are so many relevant questions on SO like this, but they all deal with getting **.html* from assets folder.

But I want to load html file from local folder, say "D://abc.html" because if my html is around 10Mb then corresponding apk size also goes upto 10mb.

Any help appreciated.

EDIT

I tried webView.loadUrl("file:///D:/Projects/myWebsite/index.html");

but it gives Web page not available and File not found error.

like image 536
GAMA Avatar asked Apr 19 '12 07:04

GAMA


People also ask

How do I display HTML content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

Which method is used to load HTML content in WebView?

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.


1 Answers

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)

like image 68
Tony the Pony Avatar answered Oct 08 '22 22:10

Tony the Pony