opening local html file with Android Browser
Asked Answered
U

3

12

i'm trying to open a local html file using the default browser using the following code:

Uri uri = Uri.fromFile(file);
Intent browserIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
startActivity(browserIntent);

but i'm getting the following exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/SolveDroid/solution.html }

i'm confused - should i create an activity to hande the web beowser? isn't it supposed to just call its activity?

please advise :)

UPDATE: the same code works if i pass a URL like so: Uri uri = Uri.parse("http://www.metalist.co.il");

Umeh answered 3/9, 2011 at 14:41 Comment(2)
Which activity you are starting? your code says startActivity() but which one?Gantrisin
what do you mean? am i supposed to call a specific Activity? isn't the default Browser an activity already?Umeh
U
8

I found an answer for this problem... just needed to add

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

i used it with the "file://" uri by using Uri.fromfile(file) and it works (Android v.2.2.1)

Umeh answered 5/9, 2011 at 15:22 Comment(0)
A
9
    Uri uri = Uri.fromFile(file);
    Intent browserIntent = new Intent(Intent.ACTION_VIEW);
    browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
    browserIntent.setData(uri);
    startActivity(browserIntent);
Akerboom answered 10/5, 2012 at 10:41 Comment(2)
file has to be in this format: file://sdcard/somedir/somefileLaguna
Does not work on Android 3.2 nor 4.0.4 see https://mcmap.net/q/1008415/-opening-local-html-file-with-android-browser-in-android-3-x any workaround?Laguna
U
8

I found an answer for this problem... just needed to add

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

i used it with the "file://" uri by using Uri.fromfile(file) and it works (Android v.2.2.1)

Umeh answered 5/9, 2011 at 15:22 Comment(0)
C
3

try this

Intent in = new Intent(Intent.ACTION_VIEW);
            File f=new File("/sdcard/html.html");
            in.setDataAndType(Uri.fromFile(f), "text/html");
            startActivity(in);
Courser answered 22/12, 2012 at 5:16 Comment(1)
A few short years later, the /sdcard folder has disappeared (in exchange for an incomprehensible nest of "external private folders"). Where do I write the temporary file so the web browser can read it?Mccready

© 2022 - 2024 — McMap. All rights reserved.