How to Share Text and Image Using Intent

In this post, I’m going to show you how to create share intent and display chooser to send text or image to another app. To achieve this task, I will use ShareCompat.IntentBuilder which is a helper class to create intents in an elegant way.

First, let me show you how to share a text:

As you can see, it’s very simple. What is important here is that I set the type of data to be shared as “text/plain” since the data (“Text to be shared”) is a plain text. Then, I set a title for the chooser activity, and let it be displayed by calling startChooser() method. Unlike most of the intent examples you can find on the Internet, using the builder pattern provided by this approach helps to keep your code clean.

Besides text sharing intent, I will also show you how to share an image. Here it is:

Again, it’s quite simple. The type of data being shared here is set as “image/*” where “*” means the data could be any image with any file extension like “jpg” or “png”, etc. You can also specify the type as “image/jpg” or “image/png”, etc. Then, I created a URI from the image file to be shared and set it as stream data of the intent. The rest is similar to what I showed for the previous intent.

One comment Add yours
  1. Great post. Without reading this post i could never know that we can send image in intent. And i have never used ShareCompat before. Today i learned new thing. I will definitely try this. Thanks for this informative post.

Leave a Reply

Your email address will not be published. Required fields are marked *