user1793383
user1793383

Reputation: 21

what is the correct path to load a local html file to an intent in Android?

I have a web site made of static local files that I am trying to load into an intent. It uses JavaScript, so I am under the impression I have to use an intent instead of a webview.

The file is located at "assets/site_files/index.html". This is the code I am trying to use:

Uri path = Uri.parse("file:///android_asset/site_files/index.html");
Intent intent = new Intent(Intent.ACTION_VIEW, path);
startActivity(intent);

The app crashes while starting the Intent, with this as the last entry in the console:

ActivityManager: Starting: Intent

The app works fine if I load the site on a web server instead of trying to load a local version. The following works perfectly:

Uri path = Uri.parse("http://www.domain.com/test_app_static/index.html");
Intent intent = new Intent(Intent.ACTION_VIEW, path);
startActivity(intent);

However, I need it to run locally so users can run it without an internet connection. I have looked around and tried several different solutions, but none of them worked for me in the specific situation of loading a local html file into an intent. Please help!

Upvotes: 2

Views: 1501

Answers (2)

sujith
sujith

Reputation: 2421

Copy the index.html page to your app data(data/data/<your.package>) folder. You can get the location using the api Activity.getDir(), and pass MODE_WORLD_READABLE as the argument so that other applications can read the file from your app data folder. Now you can pass this path as part of the intent. Remember other applications can't access your assets folder.

Upvotes: 1

SquiresSquire
SquiresSquire

Reputation: 2414

The correct url is file:///android_asset/site_files/index.html. The reason the app may be crashing is due to the fact that the asset files can only be accessed by your app. Therefore you are going to need to display that data in a WebView.

Upvotes: 1

Related Questions