I’m creating an android application that deals with texts files. I want my users to be able to share their files using email, dropbox, … but I also want them to be able to open them in the browser if they happen to be html files.
I’m having no problem in doing both codes separately. These is the one to share a file:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));
startActivity(Intent.createChooser(intent, "Send File"));
and this is the one to open the file in the browser:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
intent.setData(Uri.parse("file://" + path);
startActivity(Intent);
My problem is that I don’t want to ask the user which option he wants beforehand. I want the intent chooser to list both type of operations. I tried using text/html instead of text/plain but the browser only listens to intents when the URI starts with http.
Any ideas on how to accomplish this?
In terms of merging the two rosters, you have no choice but to do that yourself.
However, both of your current implementations have issues:
The
text/plainone will fail for apps that do not handleEXTRA_STREAM— you should consider using aUri, perhaps pointing to aContentProviderby which you make the file available, as support for the dataUriis going to be more common.Your second one will crash on devices where this browser does not exist. Moreover, you also assume that this browser will be able to consistently load from
file://Urivalues in all versions of Android, which is not a safe assumption. If the browser authors wanted it to load a local file, they would have added support for it in the manifest. Use aWebViewwidget if you do not have an HTTP URL. No professional-grade code should referencecom.androidanywhere.