3

I wanted to know how I can send text and image to a specific whatsapp contact. I found some code to view a specific contact, but not to send data.

Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);  
i.setPackage("com.whatsapp");  
startActivity(i);

But that code just opening the chat history, but doesn't take the text and image and send it.

I also try the below code for send image and text via whatsApp but that ask for choose the contact for send

Intent shareIntent = new Intent();
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, sendString);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent. setPackage ("com. whatsapp");
startActivity(shareIntent);

If that functionality is possible then please give me a suggestion for this.

Chetan Patil
  • 542
  • 1
  • 5
  • 23

1 Answers1

1

Solution :

        Intent sendIntent = new Intent("android.intent.action.SEND");
        File f=new File("path to the file");
        Uri uri = Uri.fromFile(f);
        sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
        sendIntent.setType("image");
        sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
        sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("919xxxxxxxxx")+"@s.whatsapp.net");
        sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
        startActivity(sendIntent);

Refer to my answer at for detailed explanation. https://stackoverflow.com/a/41805567/3989718

Community
  • 1
  • 1
Bhavita Lalwani
  • 903
  • 1
  • 9
  • 19