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