Add “Share Image to Whatsapp” button in Android

In android we can use the default share intent to share our image to the app installed in our app(code is given below), but many times we only need specific application like whatsapp, Facebook, twitter, gmail, etc. So for that we need to do little modification in default intent for sharing.

Default sharing intent in android

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/path-to-your-image.jpg"));
    startActivity(Intent.createChooser(share, "Share Image"));

Now if we want to share this image only to any specific app like here is whatsapp, then we need to add filter that provides package name of the app. See the code below.

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/path-to-your-image.jpg"));
share.setPackage("com.whatsapp");//package name of the app
startActivity(Intent.createChooser(share, "Share Image"));

Now you can set package name of any app that can able to share image like Facebook, twitter, gmail, etc.

Download Source Code: https://github.com/dakshbhatt21/a-computer-engineer

Advertisements

4 thoughts on “Add “Share Image to Whatsapp” button in Android

  1. Damien February 9, 2016 / 6:05 am

    Does this work in kitkat and above or only pre 4.4?

    • dakshbhatt February 9, 2016 / 6:17 am

      Yes Damien, I tried it on Kitkat and Lollipop as well as Jellybean. But didn’t try on Honeycomb or previous.

  2. Akram November 18, 2016 / 12:21 pm

    How to share Image from Android App which is coming from url to whatsapp and messenger Programmatically

    • dakshbhatt November 20, 2016 / 10:47 am

      Hi Akram! You can first download the image from url using this: http://stackoverflow.com/a/18211171/1765573

      Then you can share it to Whatsapp by above article and for messenger, you need to change the package name of Whatsapp to messenger.

      You can just open the app with your image, you can not post it to your friend or group in Whatsapp or messenger.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s