How can I send multiple data types using Intent.ACTION_SEND_MULTIPLE when using shareIntent?

The documentation is clear enough 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 looks like from this line: shareIntent.setType("image/*"); that all parts must be of the same data type. What if I wanted to send an image (image / jpeg) and a hashtag that should match the caption (text / plain)?

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

+5
source share
2 answers

If you want to share one image 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...")); 
+8
source

It is not clear from the question whether you want to send several images or just one image, but with the corresponding text.

In the first case (several images):

Use ACTION_SEND_MULTIPLE and specify the uris list as EXTRA_STREAM , as in:

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

If this is the second case (image plus text):

Use only ACTION_SEND and specify 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 exchange streams of different MIME types (for example, wallpapers and other attachments), just use the more general MIME type, for example */* . For instance:

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

From the ACTION_SEND_MULTIPLE documentation (my attention):

Several types are supported, and receivers should handle mixed types as soon as possible. The correct way for the recipient to check them is to use a content resolver for each URI. The sender of the intent should try to put the most specific type of mime in the type of intent, but it may drop back to <type>/* or */* as necessary.

eg. if you send image/jpg and image/jpg , the type of intent may be image/jpg , but if you send image/jpg and image/png , then the type of intent should be image/* .

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

+7
source

Source: https://habr.com/ru/post/1208973/


All Articles