earb
earb

Reputation: 11

Loading local html file in Android browser through intents

I need to parse a local HTML file in the google chrome browser. I have read that a way of doing this, is to copy the HTML file into the SD card and load it. What I want to do is, open the HTML file in the chrome browser from the MyProject/assets folder.

I am currently loading the html through a url. Loading the html locally would make the page load faster. This is what I am doing right now.


    Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
    i.addCategory("android.intent.category.LAUNCHER");
    i.setData(Uri.parse("http://www.example.com"));
    startActivity(i);

What I want to do is something like this


    Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
    i.addCategory("android.intent.category.LAUNCHER");
    i.setData(Uri.parse("MyProject/assets/htmlfile.html"));
    startActivity(i);

Upvotes: 1

Views: 1482

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006539

First, get rid of the setComponent() call. Treat your users with respect, and allow them to use whatever browser they wish. Besides, Chrome is only on a small percentage of Android devices, meaning that your app will crash on most devices.

Second, get rid of the addCategory() call, and replace your Intent action with Intent.ACTION_VIEW.

All of that should be done regardless of where you are getting your Web content from, as your current approach is incorrect and unreliable.

With respect to assets, other apps cannot directly read your assets via a Uri. You can create a ContentProvider that serves your assets, but Chrome does not support the content:// Uri scheme. In fact, Chrome only supports http:// and https:// Uri values for loading arbitrary URLs -- it does not even seem to support file:// very well.

If you want to view an asset, display it in a WebView widget. There, you can use a file:///android_asset/ URL to reference one of your own app's assets.

Upvotes: 1

Related Questions