1

My app is generating a HTML file, which I then want to show to the user, my code is as follows -

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

It then shows me the "Complete action using" but only lists FireFox browser. I have Chrome, Opera & Dolphin browsers installed as well. Why dont I get to choose all of them ? Thank you.

daveD
  • 869
  • 1
  • 7
  • 24
  • p.s. I removed "intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");" as it causes ForceClose on some devices. – daveD Jul 23 '13 at 18:43

4 Answers4

5

I think its possible to make them all work from a single intent using a chooser. I so far have found 3 slightly different intents -

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

Its possible to put ALL of these intents into a single chooser, using the solution offered here - How to make an intent with multiple actions

I'm keeping logcat's answer as accepted, as it showed me where I needed to go. Thanks.

Community
  • 1
  • 1
daveD
  • 869
  • 1
  • 7
  • 24
2

You can use rooted phone, grab Chrome apk, use apktool to take a look inside a manifest. There you will see that Chrome support only schemes http/https/about/javascript usually, and file scheme only once in the following intent filter:

<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>

So you can try to change mime type and do the same investigation for other browsers.

logcat
  • 3,435
  • 1
  • 29
  • 44
  • I changed my code to "browserIntent.setDataAndType(uri, "multipart/related");" & removed the line "browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);" & it opens in Chrome immediately !! I just need to determine what all the other browser require. – daveD Jul 23 '13 at 20:57
  • You might not be so lucky to create one intent for all browsers. So for different browsers you will have different intents, you query each of them by PackageManager.queryIntentActivities and then unite them all in custom dialog. – logcat Jul 24 '13 at 14:28
  • Yes, that's what I am now doing. Thanks for pointing me in the right direction. – daveD Jul 24 '13 at 18:46
1

Those other apps do not support the file:// scheme, presumably. There is no guarantee that the device will have a browser capable of loading local files.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    If I click the HTML file in Astro File Manager, I get an option to open in Dolphin, FireFox or HTML Viewer. Astro must be doing something I am not. – daveD Jul 23 '13 at 20:46
1

Depending on your application result, if really don't have needing, you can do a WebView and avoid this work on choose a browser to open your html file. Is simple, you can create a web_file_reader.xml with the following code:

<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>

And so, on your class onCreate callback method, do:

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

Set a button to open the following intent from your main activity:

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

This will open any html file with any configuration of layout and/or java script depending for you O.S version on a new Activity.

pinckerman
  • 4,115
  • 6
  • 33
  • 42