1. Sharing Text Content
ACTION_SEND is most commonly used to send text content from one Activity to another. For example, Android's built-in browser can share the URL of the currently displayed page as text content to other programs. This feature is very useful for sharing articles or websites to friends via email or social networking. Here is a Sample Code:
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);
If there is a program on the device that matches ACTION_SEND and MIME type text/plain, the Android system will execute it immediately. If there are multiple matching programs, the system will filter them out and present the Dialog to the user for selection.
If Intent.createChooser() is invoked for intent, Android always shows options. This has some advantages:
- Even if the user had previously set default action s for this intent, the selection interface would still be displayed.
- If there is no matching program, Android will display system information.
- We can specify the title of the selected interface.
Here is the updated code:
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(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to));
2. Sharing binary content
Sharing binary data requires setting a specific MIME type in conjunction with placing URI of data in EXTRA_STREAM'. Here is an example of sharing pictures, which can also be modified to share any type of binary data:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
3. Send Multiple Pieces of Content
In order to share many different types of content at the same time, you need to use ACTION_SEND_MULTIPLE with a list of URIs specified to those data. MIME types vary according to the mixed content shared. For example, if you share three JPEG images, the MIME type is still image/jpeg. If it's in different image formats, it's better to use image / to match activities that can receive any image type. If you need to share many different types of data, you can use /* to represent MIME. As described earlier, it depends on the programs that receive it to parse and process our data. Here is an example:
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));
Of course, make sure that the URIs specified to the data are accessible by the receiving program (add access rights).