2013-07-23 69 views
1

我的應用程序生成的HTML文件,然後我想展現給用戶本地的HTML文件,我的代碼如下 -安卓:查看沒有顯示所有的瀏覽器

Uri uri = Uri.parse("file://" + fileName); 
Intent browserIntent = new Intent(Intent.ACTION_VIEW); 
browserIntent.setDataAndType(uri, "text/html"); 
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE); 
startActivity(browserIntent); 

然後,它顯示了我「使用「完成操作」,但僅列出FireFox瀏覽器。我也安裝了Chrome,Opera & Dolphin瀏覽器。爲什麼我不能選擇所有這些?謝謝。

+0

p.s.我刪除了「intent.setClassName(」com.android.browser「,」com.android.browser.BrowserActivity「);」因爲它會在某些設備上導致ForceClose。 – daveD

回答

2

您可以使用固定電話,抓住Chrome瀏覽器apk,使用apktool來查看清單內部。在那裏,你會看到瀏覽器只支持計劃HTTP/HTTPS /關於以下意圖過濾器/ JavaScript的通常,和文件方案只有一次:

<intent-filter> 
    <action android:name="android.intent.action.VIEW"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
    <data android:mimeType="multipart/related" android:scheme="file"/> 
</intent-filter> 

所以,你可以嘗試改變MIME類型和做同樣的調查其它瀏覽器。

+0

我將我的代碼更改爲「browserIntent.setDataAndType(uri,」multipart/related「);」 &移除了「browserIntent.addCategory(Intent.CATEGORY_BROWSABLE)」這一行;「 &立即在Chrome中打開!我只需要確定所有其他瀏覽器需要什麼。 – daveD

+0

您可能沒有那麼幸運爲所有瀏覽器創建一個意圖。因此,對於不同的瀏覽器,您將有不同的意圖,您可以通過PackageManager.queryIntentActivities查詢它們中的每一個,然後將它們全部組合到自定義對話框中。 – logcat

+0

是的,這就是我現在正在做的。感謝您指點我正確的方向。 – daveD

1

這些其他應用程序不支持file://方案,據推測。不能保證設備將具有能夠加載本地文件的瀏覽器。

+1

如果我點擊Astro文件管理器中的HTML文件,我可以選擇在Dolphin,FireFox或HTML Viewer中打開。 Astro必須做我不是的事。 – daveD

5

我認爲有可能通過使用選擇器來使它們都能從單一意圖中工作。我到目前爲止已經找到3個稍微不同的意圖 -

// chrome ?? 
    Intent intent1 = new Intent(Intent.ACTION_VIEW);   
    intent1.setDataAndType(uri, "multipart/related"); 

    // default "Internet" browser 
    Intent intent2 = new Intent(Intent.ACTION_VIEW, uri); 
    intent2.setDataAndType(uri, "text/html"); 
    intent2.setClassName("com.android.browser", "com.android.browser.BrowserActivity");   

    // any other browser (FireFox/HTML Viewer) ?? 
    Intent intent3 = new Intent(Intent.ACTION_VIEW); 
    intent3.setDataAndType(uri, "text/html"); 
    intent3.addCategory(Intent.CATEGORY_BROWSABLE); 

它能夠把所有這些意圖到一個單一的選擇器,使用該解決方案在這裏提供的 - How to make an intent with multiple actions

我保持的logcat的答案被接受,因爲它向我展示了我需要去的地方。謝謝。

1

根據您的應用程序結果,如果真的沒有需要,您可以做一個WebView並避免這項工作,選擇一個瀏覽器來打開您的html文件。很簡單,你可以創建一個web_file_reader.xml用下面的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".WebActivity" > 

    <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/web_viewer1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

    <Button 
     android:id="@+id/but_leave" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:text="leave page" /> 

</RelativeLayout> 

所以,在你的類onCreate回調方法,這樣做:

protected void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.relatorios_activity); 

Button leave = (Button) findViewById(R.id.but_leave); 
sair.setOnClickListener(new View.OnClickListener() 
{   
    @Override 
    public void onClick(View v) 
    { 
     finish(); 
    } 
    }); 

    String fileURL = "file://" + filePath; //it's your file path name string 

    WebView webView = (WebView) findViewById(R.id.wev_viewer1); 
    webView.setVerticalScrollBarEnabled(true); 
    webView.setHorizontalScrollBarEnabled(true); 
    webView.getSettings().setBuiltInZoomControls(true); 
    webView.loadUrl(fileURL);  
} 

設置一個按鈕,打開下面的意圖您的主要活動:

Intent browserView = new Intent(/*your main activity context*/,WebActivity.class); 
startActivity(browserView); 

這將打開取決於你上了一個新的活動操作系統版本的佈局和/或Java腳本的任何配置中的任何HTML文件。

+0

ps。要添加javascript配置,請設置愚蠢代碼: webView.getSettings()。setJavaScriptEnabled(true); – user2827212