Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sent multiple data types with Intent.ACTION_SEND_MULTIPLE when using a shareIntent?

Its pretty clear in the documentation that you can send multiple pieces of data with:

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.."));

but it seems, from that one line: shareIntent.setType("image/*"); that all pieces have to be the same data type. What If I wanted to send a picture(image/jpeg) and a hashtag that should go along with in the caption (text/plain)?

How would I handle multiple kinds of content in one shareIntent? Is it possible to send 2 shareIntents to the same activity? How would I handle this?

like image 497
BigBoy1337 Avatar asked Dec 13 '14 23:12

BigBoy1337


2 Answers

If your goal is to share one picture with text, this is the code I would suggest:

String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("file://my_picture");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));
like image 91
Inneke De Clippel Avatar answered Nov 07 '22 23:11

Inneke De Clippel


It's not exactly clear from the question whether you want to send multiple images or just a single image, but with an associated text.

In the first case (multiple images):

Use ACTION_SEND_MULTIPLE and provide the list of uris as EXTRA_STREAM, as in:

Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");

If it's the second case (image plus text):

Use just ACTION_SEND and provide both EXTRA_STREAM and EXTRA_TEXT, for example:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");

If, however, you need to share streams of varying MIME types (such as both pictures and other attachments) just use a more generic MIME type, such as */*. For example:

shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
shareIntent.setType("*/*");

From the documentation of ACTION_SEND_MULTIPLE (emphasis mine):

Multiple types are supported, and receivers should handle mixed types whenever possible. The right way for the receiver to check them is to use the content resolver on each URI. The intent sender should try to put the most concrete mime type in the intent type, but it can fall back to <type>/* or */* as needed.

e.g. if you are sending image/jpg and image/jpg, the intent's type can be image/jpg, but if you are sending image/jpg and image/png, then the intent's type should be image/*.

This works when mixing, say, images and downloaded files.

like image 33
matiash Avatar answered Nov 07 '22 23:11

matiash