Share image and text in Android

Image and Text Share

Android Share Text and Images Using Share Intent

Android provide us an Intent by which we can use to share image, video, audio, text, etc. to other installed apps like WhatsApp, Facebook, Twitter, etc. We had seen many apps has functionality of sharing image and text to other app and also they have functionality to make other apps share their text and image to the app like WhatsApp from which we can share text and image and also we can share text and image to WhatsApp.

For more information about this Sharing Intent you can read over here.

Click here to create your best canvas prints.

Today we are going to create an app that share text and image to other app and also the other app can share their image and text to this app.

Example

In this tutorial, we are going to learn how to share image and text to other apps and help other apps to share image and text to our app.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is ShareActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

3. Now, create an xml layout naming share_activity.xml for ShareActivity.java. In this layout i had taken ImageView to display the selected image from gallery and share button for image and one edittext for user input to share the text.

4. For sharing image we have to follow some steps :

  • ACTION_SEND – This intent will start the Send Activity.
  • setType(“image/*”) – We have to set type of send data i.e. for image it is” image/*”.
  • putExtra(Intent.EXTRA_STREAM, imagePath) – Put extra will put the extra stream with the path name of image that we are sharing.
  • startActivity(Intent.createChooser(sharingIntent, “Share Image Using”)) – Now start chooser activity with title name.

For sharing text also we have to follow some steps :

  • setType(“text/plain”)  – We have to change the type of send because we are sending text.
  • putExtra(Intent.EXTRA_SUBJECT, “Subject Here”) – We can add extra subject to our text.
  • putExtra(Intent.EXTRA_TEXT, text) – Now put extra text that we want to share.
  • startActivity(Intent.createChooser(sharingIntent, “Share Text Using”)) – Finally start chooser activity for text share.

5. Finally come to your ShareActivity.java and add the following code. In this code i had also implemented to choose an image from gallery for image sharing. If you don’t know how to pick an image from gallery you can read my earlier tutorial.

6. Create a new class naming Utils.java for decoding large image into smaller image while picking an image from gallery.

 

Still we had learned how to share an image and text to other apps. Now we are going to learn how to make our app in such a way so that other apps can also share image and text.

7. For other apps to share image and text we have to create a layout to display image and text. I named this layout shared_activity.xml.

8. Now create a new class naming SharedActivity.java for sharing text  and image to this activity. In this activity we get the intent action and action type and check the conditions according to text and image and display according to it.

9. Finally the main thing is to add this SharedActivity.java to Manifest file so that your activity should list in Sharing listview. For this you have to add some intent filter and data type that activity gonna listen.

Your Manifest will look like this :

10. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

Post comment

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