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

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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?

回答1:

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


回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!