27

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.

Community
  • 1
  • 1
GAMA
  • 5,958
  • 14
  • 79
  • 126

4 Answers4

47

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)

Community
  • 1
  • 1
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • `webView1.loadUrl("file:///C:/Users/myName/Desktop/e-magzine/index.html");` but it gives `Web page not available. File not found` message. – GAMA Apr 19 '12 at 08:41
  • 4
    Android is a Linux implementation, so Windows-style paths won't work. Your path is relative to the home folder of your app, but I'll look for a reference that discusses this further – Tony the Pony Apr 19 '12 at 10:22
  • I got what you said. So going by this logic, `file:///` should be referring to root folder of project. So how do I use files from external resources i.e. from local drive? – GAMA Apr 19 '12 at 10:24
  • Instead of a "local drive," there are 3 types of file locations in Android. I've updated my answer with examples on constructing the URL for each. – Tony the Pony Apr 19 '12 at 10:38
  • But in any case, my html files and corresponding resources i.e. images, videos, etc. will be placed **within the folder** and hence *.apk size will be approx. equal to size of HTML which can go upto 50Mb also. – GAMA Apr 19 '12 at 10:54
  • When you deploy content with your app, you'll either have to put it in the apk or download it after the app is installed. I assume you are trying to test your app in the emulator, and don't want the hassle of packaging up a large apk for each debug run? – Tony the Pony Apr 19 '12 at 10:58
  • hmmmm. So the easier way would be put it into the assets? Also rendering of HTML into the webview is not that simplistic. I need to set initial scale to 50 in order to view the content properly – GAMA Apr 19 '12 at 11:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10272/discussion-between-tony-the-pony-and-gama) – Tony the Pony Apr 19 '12 at 11:08
5

In Android 4.4 KitKat, a "Not allowed to load local resource: file:///.." is thrown. arise when loadURL and the only alternative I've found is "loadDataWithBaseURL".

webView.loadDataWithBaseURL("file:///android_asset/demo/",
                             tmpDocumentText,"text/html", "UTF-8", null);
Toni Gamez
  • 6,819
  • 1
  • 23
  • 18
  • `wbHelp.loadUrl("file:///android_asset/Help.html");` works in 5.1+ not sure if you need write external storage if it's internal – behelit Jul 28 '16 at 01:20
3

WebView has loadData method http://developer.android.com/reference/android/webkit/WebView.html

All you need to do is reading the file into String then feed it to WebView using loadData.

RobGThai
  • 5,937
  • 8
  • 41
  • 59
0

either one can use this way to load file specifically if the file is present within android studio project (pls note android_res is res folder actually and should not be replaced)

webView.loadUrl("file:///android_res/drawable/FILENAME.EXTENSION");

but if that is not working please try another way of loading file like

webView.loadDataWithBaseURL("file:///android_asset/demo/",tmpDocumentText,
                         "text/html", "UTF-8", null);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103