Android: Share plain text using intent (to all messaging apps)
Use the code as:
/*Create an ACTION_SEND Intent*/ Intent intent = new Intent(android.content.Intent.ACTION_SEND); /*This will be the actual content you wish you share.*/ String shareBody = "Here is the share content body"; /*The type of the content is text, obviously.*/ intent.setType("text/plain"); /*Applying information Subject and Body.*/ intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject)); intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); /*Fire!*/ startActivity(Intent.createChooser(intent, getString(R.string.share_using)));
New way of doing this would be using ShareCompat.IntentBuilder like so:
// Create and fire off our Intent in one fell swoopShareCompat.IntentBuilder // getActivity() or activity field if within Fragment .from(this) // The text that will be shared .setText(textToShare) // most general text sharing MIME type .setType("text/plain") .setStream(uriToContentThatMatchesTheArgumentOfSetType) /* * [OPTIONAL] Designate a URI to share. Your type that * is set above will have to match the type of data * that your designating with this URI. Not sure * exactly what happens if you don't do that, but * let's not find out. * * For example, to share an image, you'd do the following: * File imageFile = ...; * Uri uriToImage = ...; // Convert the File to URI * Intent shareImage = ShareCompat.IntentBuilder.from(activity) * .setType("image/png") * .setStream(uriToImage) * .getIntent(); */ .setEmailTo(arrayOfStringEmailAddresses) .setEmailTo(singleStringEmailAddress) /* * [OPTIONAL] Designate the email recipients as an array * of Strings or a single String */ .setEmailTo(arrayOfStringEmailAddresses) .setEmailTo(singleStringEmailAddress) /* * [OPTIONAL] Designate the email addresses that will be * BCC'd on an email as an array of Strings or a single String */ .addEmailBcc(arrayOfStringEmailAddresses) .addEmailBcc(singleStringEmailAddress) /* * The title of the chooser that the system will show * to allow the user to select an app */ .setChooserTitle(yourChooserTitle) .startChooser();
If you have any more questions about using ShareCompat, I highly recommend this great article from Ian Lake, an Android Developer Advocate at Google, for a more complete breakdown of the API. As you'll notice, I borrowed some of this example from that article.
If that article doesn't answer all of your questions, there is always the Javadoc itself for ShareCompat.IntentBuilder on the Android Developers website. I added more to this example of the API's usage on the basis of clemantiano's comment.
This a great example about share with Intents in Android:
* Share with Intents in Android
//Share text:Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_SEND);intent2.setType("text/plain");intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" ); startActivity(Intent.createChooser(intent2, "Share via"));//via Email:Intent intent2 = new Intent();intent2.setAction(Intent.ACTION_SEND);intent2.setType("message/rfc822");intent2.putExtra(Intent.EXTRA_EMAIL, new String[]{EMAIL1, EMAIL2});intent2.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" ); startActivity(intent2);//Share Files://Image:boolean isPNG = (path.toLowerCase().endsWith(".png")) ? true : false;Intent i = new Intent(Intent.ACTION_SEND);//Set type of fileif(isPNG){ i.setType("image/png");//With png image file or set "image/*" type}else{ i.setType("image/jpeg");}Uri imgUri = Uri.fromFile(new File(path));//Absolute Path of imagei.putExtra(Intent.EXTRA_STREAM, imgUri);//Uri of imagestartActivity(Intent.createChooser(i, "Share via"));break;//APK:File f = new File(path1);if(f.exists()){ Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_SEND); intent2.setType("application/vnd.android.package-archive");//APk file type intent2.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) ); startActivity(Intent.createChooser(intent2, "Share via"));}break;