Reputation: 976
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.
Upvotes: 1
Views: 2963
Reputation: 11
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.
Upvotes: 1
Reputation: 976
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.
Upvotes: 5
Reputation: 3454
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.
Upvotes: 2
Reputation: 1006539
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.
Upvotes: 1