Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does file:///android_asset/www/index.html mean?

Tags:

android

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.

like image 618
Nav Avatar asked Aug 14 '12 22:08

Nav


3 Answers

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!

like image 155
t0mm13b Avatar answered Nov 04 '22 00:11

t0mm13b


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

  1. app/src/main/assets

    directory.

like image 18
Lunero Avatar answered Nov 04 '22 00:11

Lunero


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.

like image 16
dinosaur Avatar answered Nov 04 '22 01:11

dinosaur