Cómo abrir el navegador para abrir el archivo local

Estoy tratando de enviar intento al navegador para abrir el archivo local. Deseo usar el navegador predeterminado para abrir este archivo.

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

Pero me lanza y exeption

 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 } 

Si utilizo la siguiente intención, el navegador abre google.com como se esperaba

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

También cuando escribo el archivo url (file:///sdcard/release_notes.htm) a la barra de direcciones del navegador que se abre como se esperaba.

Es necesario agregar la categoría de navegable en la intención.

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

El navegador se inicia sólo para HTML y otros archivos compatibles. Esto debería funcionar:

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

Esto es lo que está funcionando para mí. Tomé el tipo mime directamente del Manifest.xml del navegador predeterminado de Android. Al parecer, el texto / html mime es sólo para http (s) y esquemas en línea.

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

No estoy seguro de si funciona en cada combinación de android / teléfono / navegador, pero es la única manera que podría conseguir que funcione.

Editar: probado con cromo y no funciona. También no funciona con mi dispositivo 2.3.3. Parece que funciona con el navegador predeterminado en Android 4.0.

Aquí hay un enfoque ligeramente más robusto:

 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); } } 

He probado esto en un Nexus One (API 16, 4.1.2), donde el try funciona, y un Nexus 5 (API 22, 5.1.1), donde sólo funciona la catch .

Tal vez esto funciona:

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

El problema es que la nueva actividad no tiene acceso a la página html dentro de su aplicación, ya que es una aplicación diferente y no tiene permisos para hacerlo.

Este código funciona tanto en API 10 como en 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); 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.