user1723095
user1723095

Reputation: 1219

How to open local html file in default browser

I can open my local html file with android browser by following way:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(webPageUri, "text/html");
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
startActivity(intent);

And it works. But I would like to open my local html file in default browser without specifing:

intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

Is there a way to to that?

Edit:

If I remove setClassName as you suggest, it opens in HtmlViewer (it is not a default browser). And if I do it like that:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(webPageUri);
startActivity(intent);

I get: ActivityNotFoundException

Upvotes: 3

Views: 4015

Answers (2)

Anup Cowkur
Anup Cowkur

Reputation: 20563

Just use:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(webPageUri, "text/html");
startActivity(intent);

This will give the user a list of installed browsers to choose from. If there is only one browser, then that's the one that will be launched.

Upvotes: 1

iTurki
iTurki

Reputation: 16398

Just remove the setClassName() line and you are set to go.

This will launch the default browser if it is the only browser in the phone. If there are more than one, the user will have to select one.

Upvotes: 0

Related Questions