I was developing an android application for a customer. One requirement was to share some media and text with the installed social Android apps like Google+, Email, Gmail, Facebook, Drive, Messaging, WhatsApp etc. This post shows how to share predefined text and media from your app.
Overview
In Android you can share text or media with other installed apps using Intents. Below I demonstrate a simple example (sharing a predefined text) from our Panchanga app. I show the original minimal code used in the app and then give recommended, modern improvements (chooser, file sharing, FileProvider, and package checks).
Create the menu item
The original app added a menu item called "Share". The minimal code used was:
MenuItem share;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
share = menu.add("Share");
share.setIcon(R.drawable.share);
share.setOnMenuItemClickListener(this);
return true;
}
Note: share.png was added to the drawable folder.
Implement the onMenuItemClickListener (original example)
This is the original minimal onMenuItemClick implementation in the article:
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem == share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
return true;
}
Here, the system picks all the social apps installed on the device and shows them in the chooser. You can select the required app to send. The text "This is my text to send." is what is sent to other apps.
Below are the images from the original article showing the share chooser and examples:



Share only with a specific app (original note)
If you want to target only a specific app instead of showing a chooser, you can call setPackage on the Intent. Example from the original article:
sendIntent.setPackage("com.whatsapp");
The article linked to the Intent.setPackage docs: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)
"com.whatsapp" is the package name of WhatsApp; use the package name of the app you want to target.
Recommended improvements and modern best practices
Keep the original minimal examples (above) for reference, but use these improvements for production apps.
1) Always use a chooser so the user sees a consistent share sheet
Replace startActivity(sendIntent) with:
startActivity(Intent.createChooser(sendIntent, "Share via"));
This forces the chooser UI and avoids launching a default app unexpectedly.
2) If targeting a specific package, check that the package exists first
PackageManager pm = getPackageManager();
if (pm.getLaunchIntentForPackage("com.whatsapp") != null) {
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
} else {
// App not installed — fall back to chooser
startActivity(Intent.createChooser(sendIntent, "Share via"));
}
Wrap startActivity in try/catch(ActivityNotFoundException) as a final fallback.
3) Sharing files (images, PDFs) — use EXTRA_STREAM and FileProvider on Android N+
When sharing a file (image, PDF, etc.) use a content:// URI and FileProvider for API 24+ to avoid FileUriExposedException.
Example (sharing a single image):
File file = new File(getFilesDir(), "shared_image.jpg");
Uri contentUri = FileProvider.getUriForFile(context, "your.package.name.fileprovider", file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share image"));
You must declare a FileProvider in your AndroidManifest.xml and add a file_paths.xml resource describing the shared paths. See the official docs for details: https://developer.android.com/reference/androidx/core/content/FileProvider
4) Sharing multiple files
Use ACTIONSENDMULTIPLE with an ArrayList
ArrayList<Uri> uris = new ArrayList<>();
// add multiple content URIs
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images"));
5) Attach subject or title when appropriate
For email apps, add EXTRASUBJECT and/or EXTRATITLE:
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out Panchanga");
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
Note: some apps (notably Facebook) ignore prefilled text in shared content due to their platform policy.
6) MIME types and behaviour
Set the right MIME type: text/plain for text, image/* for images, application/pdf for PDFs. A wrong MIME type may hide some target apps from the chooser.
7) Runtime permissions
If the files you want to share are on external storage, ensure you handle runtime permissions (READEXTERNALSTORAGE) on Android 6.0+.
8) Troubleshooting and UX notes
- If the chooser shows no apps, check the MIME type and ensure there is at least one app that handles the action and MIME type.
- If you target a specific package and the app is not installed, handle it by notifying the user or falling back to the chooser.
Conclusion
The original minimal example is valid for sharing simple text. For robust production behavior across Android versions, add a chooser, handle URIs with FileProvider, set correct MIME types, and guard setPackage with a PackageManager check. That will give a safer and more consistent user experience.
Happy Coding!
No comments:
Post a Comment