Open html file in browser from intent

2.8k views Asked by At

I would like to open downloaded html-files via browser by passing them into an intent and starting them out of my activity.

I know that there are dozen of threads about this problem (e.g. this).

However, none of these approaches seem to work.

So far I have:

File file = //myHTMLFile;
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
browserIntent.setData(Uri.fromFile(file));

I tried setting Mimeypes like text/html, adding/removing categories and even using multipe mimetypes and the extra mimetype flag but none of these ideas worked for me.

Currently a "default" browser will open which cannot show the file. I would like to open files in Chrome, Firefox or other popular browsers.

Also weird: The Uri-String, which I receive from Uri.fromFile() is not openable by Chrome.

The path looks like file:///data/data/myapp/website.html.

The file itself is located at /storage/emulated/0/myapp/website.html. Passing this value with file:/// as prefix, Chrome will show the website.

Is Uri.fromFile correct?

1

There are 1 answers

5
CommonsWare On BEST ANSWER

The path looks like file:///data/data/myapp/website.html.

Assuming that myapp is really your application ID, this is pointing at internal storage, and no other app can access that (exception: rooted devices).

Is Uri.fromFile correct?

Uri.fromFile() works fine, but your file that you are passing in is not being created correctly.

Beyond that, please bear in mind that browsers do not have to support the file:// scheme, and so some may not.