21

I want to know what does file:/// mean while loading a html file from the assets folder in android

Is it an absolute path name which points to the root directory?

I saw this in the tutorial for phonegap by the way.

Uzair Hayat
  • 455
  • 9
  • 17
Nav
  • 9,966
  • 19
  • 54
  • 83

6 Answers6

41

file:/// is a URI (Uniform Resource Identifier) that simply distinguishes from the standard URI that we all know of too well - http://.

It does imply an absolute path name pointing to the root directory in any environment, but in the context of Android, it's a convention to tell the Android run-time to say "Here, the directory www has a file called index.html located in the assets folder in the root of the project".

That is how assets are loaded at runtime, for example, a WebView widget would know exactly where to load the embedded resource file by specifying the file:/// URI.

Consider the code example:

WebView webViewer = (WebView) findViewById(R.id.webViewer);
webView.loadUrl("file:///android_asset/www/index.html");

A very easy mistake to make here is this, some would infer it to as file:///android_assets, notice the plural of assets in the URI and wonder why the embedded resource is not working!

t0mm13b
  • 33,483
  • 8
  • 75
  • 107
  • 4
    "asset" verses "assets" was an error that took me hours to solve on my first PhoneGap/Android project. Your solution helped me realize what I was doing wrong. – Brian Stinar Jul 15 '13 at 00:18
  • 1
    Do I have to create this folder or where exactly is localize? – ncubica Feb 12 '14 at 23:36
  • 1
    You create that in `/res` a folder called `assets`. – t0mm13b Feb 13 '14 at 20:30
  • Why does it use three slashes? – Michael Levy Apr 20 '15 at 23:34
  • 2
    @MichaelLevy see http://tools.ietf.org/html/draft-kerwin-file-scheme-06 on Page 4 for your answer. It is a widely adopted de-facto standard for Uri naming conventions in regards to filesystems. Also, see http://superuser.com/questions/352133/why-do-file-urls-start-with-3-slashes as well. – t0mm13b Apr 20 '15 at 23:58
  • So is the directory structure in this case /res/assets/android_asset/www/index.html then? @t0mm13b – Paradox May 01 '18 at 15:02
18

If someone uses AndroidStudio make sure that the assets folder is placed in

  1. app/src/main/assets

    directory.

Community
  • 1
  • 1
Lunero
  • 667
  • 1
  • 8
  • 17
16

The URI "file:///android_asset/" points to YourProject/app/src/main/assets/.

Note: android_asset/ uses the singular (asset) and src/main/assets uses the plural (assets).

Suppose you have a file YourProject/app/src/main/assets/web_thing.html that you would like to display in a WebView. You can refer to it like this:

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

The snippet above could be located in your Activity class, possibly in the onCreate method.

Here is a guide to the overall directory structure of an android project, that helped me figure out this answer.

dinosaur
  • 2,924
  • 4
  • 26
  • 39
4

It is actually called file:///android_asset/index.html

file:///android_assets/index.html will give you a build error.

grg
  • 4,278
  • 3
  • 31
  • 43
sivi
  • 9,914
  • 2
  • 47
  • 49
0

It took me more than 4 hours to fix this problem. I followed the guide from http://docs.phonegap.com/en/2.1.0/guide_getting-started_android_index.md.html#Getting%20Started%20with%20Android

I'm using Android Studio (Eclipse with ADT could not work properly because of the build problem).

Solution that worked for me:

  1. I put the /assets/www/index.html under app/src/main/assets directory. (take care AndroidStudio has different perspectives like Project or Android)

  2. use super.loadUrl("file:///android_asset/www/index.html"); instead of super.loadUrl("file:///android_assets/www/index.html"); (no s)

KhalodaRK84
  • 85
  • 2
  • 14
Ranger Way
  • 529
  • 5
  • 11
0

it's file:///android_asset/... not file:///android_assets/... notice the plural of assets is wrong even if your file name is assets

Micho
  • 3,861
  • 13
  • 37
  • 40
  • Duplicate of answer provided 3 years prior: https://stackoverflow.com/a/21242233/1549818 – grg Oct 29 '20 at 12:41