How to share data to whatsapp in android

Whatsapp is one of the applications which receives shared data from other applications . In the latest update of whatsapp it is allowing other applications to share gif images.  It is quite similar to normal intent image sharing method in android. In this tutorial share data to whatsapp in android .

Content

  1. Text sharing to whatsapp in android
  2. Image sharing to whatsapp in android
  3. Gif sharing to whatsapp in android
  4. How to share data directly to contact in whatsapp in android

Text sharing to whatsapp in android

Place the following code in your share button onclick which will help you to directly open the whatsapp with the message you are going to replace in the following code. This will skip the intent popup step in which the popup will contain many application which allows sharing it directly opens whatsapp.

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Message you want to share");
try {

startActivity(whatsappIntent);

} catch (android.content.ActivityNotFoundException ex) {

Toast.makeText(getApplicationContext(),"Whatsap not installed",Toast.LENGTH_SHORT).show();

}

Image sharing to whatsapp in android

Place the following code in your share button onclick which will help you to directly open the whatsapp with the image you need to share. This will skip the intent popup step in which the popup will contain many application which allows sharing it directly opens whatsapp.

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "yourfilename.jpg";
File sharingfile = new File(baseDir, fileName);

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("image/jpg");//mention type of image you want to share
whatsappIntent.setPackage("com.whatsapp");
Uri uri = Uri.fromFile(sharingfile);
whatsappIntent.putExtra(Intent.EXTRA_STREAM, uri);
try {
startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {

Toast.makeText(getApplicationContext(),"Whatsap not installed",Toast.LENGTH_SHORT).show();
}

Gif sharing to whatsapp in android

After the usage of gif became common whatsapp also started allowing sharing gif into chat. The code is same as above which will allow you to share images. All you want to do is to change the intent type to gif. Place the following code in the onclick of button which you want to share gif.

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "yourfilename.gif";
File sharingfile = new File(baseDir, fileName);

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("image/gif");
whatsappIntent.setPackage("com.whatsapp");
Uri uri = Uri.fromFile(sharingfile);
whatsappIntent.putExtra(Intent.EXTRA_STREAM, uri);
try {
    startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {

    Toast.makeText(getApplicationContext(),"Whatsap not installed",Toast.LENGTH_SHORT).show();
}

How to share data directly to contact in whatsapp in android

In the above steps we seen how to open whatsapp directly with the sharing data  by skipping the intent popups. Here we will go one more step ahead. Suppose if you have the contact of the person to which the data to be shared, then you can directly use it in code which will open up the chat of that particular person with the message you are passing. Following is the code for doing the same. The code is for sending the text message , instead of that if you want to share any other type like images or documents you just need to change the intent type.

String receiver_number = "*********"; //number inside double quotes with country code and without + sign
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.setType("text/plain");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Message you need to send");
whatsappIntent.putExtra("jid", receiver_number + "@s.whatsapp.net");
if (whatsappIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(this, "Whatsap not installed on your phone", Toast.LENGTH_SHORT).show();
}else{
startActivity(whatsappIntent);
}

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

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