4

I am not able to send both text message and image through whatsapp . Either , I am able to start that particular contact chat thread without any message or I am only able to send message but not opening that particular contact chat thread.

I have followed following link : http://www.whatsapp.com/faq/en/android/28000012

Sending message through WhatsApp

Send Whatsapp message to specific contact

But not getting Success :(

Can anybody help me with this. How to Send a Text message and Image through whatsapp from Own Application with help of Intent in Android ?

Community
  • 1
  • 1
Ankit
  • 89
  • 1
  • 1
  • 8
  • You cant send both together..however you can send text and image seperately.!! – Dhinakaran Thennarasu Oct 29 '14 at 11:20
  • @Dhina , I am using Intent for it but no luck ... ?? not getting send a image to a particular friend which is in whats app. Note that Image and Number of friend are passed through intent . How to do this?? .. {*_*} – Ankit Oct 31 '14 at 07:53
  • @Aarastu I am also stuck on the same issue ,here is link to my ques http://stackoverflow.com/questions/30036992/android-getting-an-error-no-application-can-perform-this-action-while-trying/30045265#30045265 :( – salil vishnu Kapur May 05 '15 at 09:20
  • Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(Intent.EXTRA_TEXT, your_text_here); startActivity(Intent.createChooser(sharingIntent, "share via")); } – Vaishali Sharma May 05 '15 at 10:13

4 Answers4

8

Try using the following 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);

EXTRA INFORMATION ABOUT THE PROCESS OF FINDING THE SOLUTION :

After reverse engineering WhatsApp, I came across the following snippet of Android manifest,

Normal Share intent, uses "SEND" which does not allow you to send to a specific contact and requires a contact picker.

The specific contact is picked up by Conversation class and uses "SEND_TO" action, but it uses sms body and can not take up image and other attachments.

 <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.Conversation" android:theme="@style/Theme.App.CondensedActionBar" android:windowSoftInputMode="stateUnchanged">
            <intent-filter>
                <action android:name="android.intent.action.SENDTO"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="sms"/>
                <data android:scheme="smsto"/>
            </intent-filter>
        </activity>

Digging further, I came across this,

 <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:name="com.whatsapp.ContactPicker" android:theme="@style/Theme.App.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.PICK"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="com.whatsapp"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="audio/*"/>
                <data android:mimeType="video/*"/>
                <data android:mimeType="image/*"/>
                <data android:mimeType="text/plain"/>
                <data android:mimeType="text/x-vcard"/>
                <data android:mimeType="application/pdf"/>
                <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"/>
                <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
                <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation"/>
                <data android:mimeType="application/msword"/>
                <data android:mimeType="application/vnd.ms-excel"/>
                <data android:mimeType="application/vnd.ms-powerpoint"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="audio/*"/>
                <data android:mimeType="video/*"/>
                <data android:mimeType="image/*"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:host="send" android:scheme="whatsapp"/>
            </intent-filter>
            <meta-data android:name="android.service.chooser.chooser_target_service" android:value=".ContactChooserTargetService"/>
        </activity>

Finally using a decompiler for ContactPicker and Conversation class,the extra key-value for phone number was found to be "jid".

Bhavita Lalwani
  • 903
  • 1
  • 9
  • 19
  • Nice find! I tried and it works. Is there any ways to send the message automatically? Currently, it pastes the message in the box and I still need to hit the send icon to actually send the message. Thanks a lot! – Lê Hoàng Feb 06 '17 at 04:12
  • Thanks as of now, I have not been able to find a solution to automatically send the message without the send key. Because what we are doing is sharing the message with the whatsapp and lettting it handle the situation. I will dig further and update if I am able to find a workaround. Thank you for raising an interesting question. – Bhavita Lalwani Feb 06 '17 at 07:46
  • It should be noted that you have to pass PhoneNumberUtils.stripSeparators([COUNTRY_CODE]+[PHONE_NUMBER])+"@s.whatsapp.net". It's not entirely clear what 919 means. By the way, it still works in version 2.18.277 – javmarina Sep 20 '18 at 17:16
2

Earlier it wasn't possible but since the May '15 update. Checkout :

try{
    PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    String sendString = "some random string";
    sendIntent.setPackage("com.whatsapp");
    sendIntent.putExtra(Intent.EXTRA_TEXT, sendString);
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    sendIntent.setType("image/*");
    startActivity(sendIntent);
} catch (Exception e){
    // some code
}

Here PackageInfo line is just to check if WhatsApp is installed. It throws Exception if not. You can just ignore that if you want to do a normal share (and setPackage also).

Also. It is important that the media you want to share has to be publicly available on local storage.

UPDATE

To send to a specific contact

Uri uri = Uri.parse("smsto:" + "<CONTACT_NUMBER>");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);

As Action Send To is now allowed.

Manan Sharma
  • 631
  • 2
  • 14
  • 28
  • You can refer to my previous answer http://stackoverflow.com/questions/15462874/sending-message-through-whatsapp. Thanks to @ArulxZ for a better approach to package discovery. Also, Sending to a specific contact is still not possible – Manan Sharma May 08 '15 at 21:17
  • A̶l̶s̶o̶,̶ ̶S̶e̶n̶d̶i̶n̶g̶ ̶t̶o̶ ̶a̶ ̶s̶p̶e̶c̶i̶f̶i̶c̶ ̶c̶o̶n̶t̶a̶c̶t̶ ̶i̶s̶ ̶s̶t̶i̶l̶l̶ ̶n̶o̶t̶ ̶p̶o̶s̶s̶i̶b̶l̶e̶ ̶ Now Possible >> Uri uri = Uri.parse("smsto:" + "9888873438"); Intent i = new Intent(Intent.ACTION_SENDTO, uri); i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage); – Manan Sharma Jan 21 '16 at 12:57
0

I saw many similar questions here by many peoples, and the answer is "Yes, this is possible". You can send messages directly to Whatsapp from your app without opening Whatsapp, and this can be achieved by using Wear Api and RemoteInput method.

It is a bit complicated task, so for reference you can use this code. It does the same that you are searching for.

Dat Nguyen
  • 1,626
  • 22
  • 25
Baljeet
  • 84
  • 3
0
public static void shareFileOnWhatsapp(Context context, Uri fileUri, String message, String mobile){
        try {
            String number = mobile.replace("+", "").replace(" ", "");
            number = "91" + number;
            Intent intentShareFile = new Intent(Intent.ACTION_SEND);
            intentShareFile.setType("*/*");
            intentShareFile.putExtra(Intent.EXTRA_STREAM, fileUri);
            intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
                    "Share");
            if (JUtil.isNullOrEmpty(message)) {
                message = " ";
            }
            intentShareFile.putExtra(Intent.EXTRA_TEXT, message);
            intentShareFile.putExtra("jid", number + "@s.whatsapp.net");
            intentShareFile.setPackage("com.whatsapp");
            context.startActivity(Intent.createChooser(intentShareFile, "Share"));
        } catch (Exception e) {
            Log.e("SHARE", "Exception occured sharing file", e);
        }
    }