How to launch a browser to open a local file

I am trying to send the intention to the browser to open a local file. I want to use the default browser to open this file.

if(file.exists()){ Log.d(TAG, "file.exists"); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); context.startActivity(intent); } 

But it leaves me and sets me free

 08-10 13:27:58.993: ERROR/AndroidRuntime(28453): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/release_notes.htm } 

If I use the following browser, the browser opens google.com as expected

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com")); 

Also, when I write the url (file:///sdcard/release_notes.htm) to the address bar of the browser, it opens it as expected.

+7
source share
7 answers

You need to add a viewing category to the intent.

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); intent.addCategory(Intent.CATEGORY_BROWSABLE); startActivity(intent); 
+4
source

The browser runs only for HTML and other compatible files. this should work:

 Intent intent = new Intent(ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "text/html"); 
+7
source

This is what works for me. I took the mime type directly from Manifest.xml of the default Android browser. Apparently mime text / html is for http (s) and inline schemes only.

 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_BROWSABLE); intent.setDataAndType(Uri.fromFile(filePath), "application/x-webarchive-xml"); startActivity(intent); 

Not sure if it works in every combination of Android and phone / browser, but this is the only way to make it work.

Edit: Tested with chrome and not working. Also does not work with my device 2.3.3. It seems to work with the default browser in Android 4.0.

+6
source

Here's a tougher approach:

 private void openInBrowser(File file) { final Uri uri = Uri.fromFile(file); try { final Intent browserIntent = new Intent(Intent.ACTION_VIEW); browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); browserIntent.setData(uri); startActivity(browserIntent); } catch (ActivityNotFoundException e) { final Intent browserIntent = new Intent(Intent.ACTION_VIEW); browserIntent.setDataAndType(Uri.fromFile(file), "text/html"); startActivity(browserIntent); } } 

I tested this on Nexus One (API 16, 4.1.2), where try works, and Nexus 5 (API 22, 5.1.1), where only catch works.

+2
source

Maybe this works:

 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "text/html"); startActivity(intent); 
+1
source

The problem is that the new activity does not have access to the html page inside your application, since it is a different application and it does not have permissions to do this.

0
source

This code works with both API 10 and API 11.

 File f = new File("/mnt/sdcard/index.html"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_BROWSABLE); intent.setDataAndType(Uri.fromFile(f), "application/x-webarchive-xml"); // Have to add this one in order to work on Target 2.3.3 (API 10) intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); startActivity(intent); 
0
source

All Articles