1

How can we send an image directly to whatsapp from android app? I tried using

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Uri imageuri = getImageUri(getApplicationContext(), b);
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.setPackage("com.whatsapp");
                sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                sendIntent.putExtra(Intent.EXTRA_STREAM, imageuri);

                sendIntent.setType("image/*");
                startActivity(sendIntent);

the above code opens send window of whatsapp. Is there any other way so that image will directly be sent without opening whats app window?

Anagha Magar
  • 11
  • 1
  • 5
  • That's completely up to the WhatsApp devs. If the way they've implemented a photo sharing api is to have the intent open the WhatsApp window, that's what is going to happen. – jwkicklighter Aug 31 '15 at 10:27
  • without opening whatsapp window, then whom to send image..? – Praveen B. Bhati Aug 31 '15 at 10:28
  • without opening whats app send window. Like using facebook sdk we can share image without actually going to facebook. – Anagha Magar Aug 31 '15 at 10:31
  • @AnaghaMagar check http://stackoverflow.com/questions/24278082/send-whatsapp-message-in-background-or-send-message-and-close-the-app-in-android?rq=1 , what you're trying to do seems to be not possible – arodriguezdonaire Aug 31 '15 at 13:51

1 Answers1

3

Android intent system

Like most social apps on Android, WhatsApp listens to intents to share media and text. Simply create an intent to share text, for example, and WhatsApp will be displayed by the system picker:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

However, if you prefer to share directly to WhatsApp and bypass the system picker, you can do so by using setPackage in your intent:

sendIntent.setPackage("com.whatsapp");
This would simply be set right before you call startActivity(sendIntent);

Similarly you can use Android's intent system to send media through WhatsApp, once again, using setPackage to restrict the package to WhatsApp if you only want to send to WhatsApp. Check this developer page for more information.

Custom URL Scheme

WhatsApp provides a custom URL scheme to interact with WhatsApp:

If you have a website and want to open a WhatsApp chat with a pre-filled message, you can use our custom URL scheme to do so. Opening whatsapp://send?text= followed by the text to send, will open WhatsApp, allow the user to choose a contact, and pre-fill the input field with the specified text.

Here is an example of how to write this on your website:

<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

// NOT ORIGINAL TEXT: You can call this endpoint through your Java code too

Found in: https://www.whatsapp.com/faq/es/android/28000012

arodriguezdonaire
  • 5,396
  • 1
  • 26
  • 50