2011-08-10 117 views
7

我試圖發送意圖瀏覽器打開本地文件。我希望使用默認瀏覽器來打開這個文件。如何啓動瀏覽器打開本地文件

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

但是,如果使用下面的意圖瀏覽器,它拋出我和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 } 

打開google.com預期

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

而且當我寫文件的URL (file:///sdcard/release_notes.htm)到瀏覽器地址它會按預期打開它。

回答

4

您需要在意圖中添加可瀏覽的類別。

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

似乎沒有幫助 '08-10 14:03:48.414:ERROR/AndroidRuntime(29612):android.content.ActivityNotFoundException:找不到處理Intent的動作{act = android.intent.action.VIEW cat = [android.intent.category.BROWSABLE] dat = file:///sdcard/release_notes.htm}' – roose

+0

嗯,怪異的,我似乎沒有任何問題,在我的手機上:(請仔細檢查你的當你嘗試這個時,sdcard沒有安裝到電腦上。 – Zharf

+2

也許你可以嘗試添加瀏覽器的類名顯式給intent:'intent.setClassName(「com.android.browser」,「com.android.browser.BrowserActivity」); ' – Zharf

7

瀏覽器僅針對HTML和其他兼容文件啓動。這應該工作:

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

它也可以。謝謝! – roose

+0

太棒了!它像一個魅力:) – evya

1

也許這個作品:

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

這是什麼爲我工作。我直接從Android的默認瀏覽器的Manifest.xml中獲取了MIME類型。顯然,text/html mime僅適用於http(s)和inline scheme。

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

不知道它是否適用於所有的android /手機/瀏覽器組合,但它是唯一能讓它工作的方法。

編輯:使用鉻進行測試,不起作用。也不適用於我的2.3.3設備。似乎與Android 4.0中的默認瀏覽器一起工作。

0

問題是,新的活動無法訪問您的應用程序內的HTML頁面,因爲它是一個不同的應用程序,它沒有權限這樣做。

0

此代碼工作在兩個API 10和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); 
2

這裏有一個性能稍微更可靠的方法:

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

我在歌Nexus One(API 16測試了這個,4.1 .2),其中try工作,和一個Nexus 5(API 22,5.1.1),其中只有catch工作。

相關問題