Java – How can I send text messages and images from my own application via Whatsapp with the help of Intent in Android?

How can I send text messages and images from my own application via Whatsapp with the help of Intent in Android?… here is a solution to the problem.

How can I send text messages and images from my own application via Whatsapp with the help of Intent in Android?

I can’t send text messages and images via WhatsApp. Or, I can
Start that particular contact chat thread without any messages, or I can only send messages but can’t open that particular contact chat thread.

I clicked on the following link:
http://www.whatsapp.com/faq/en/android/28000012

Sending message through WhatsApp

Send Whatsapp message to specific contact

But didn’t work out 🙁

Can anyone help me with this.
How can I send text messages and images from my own app via WhatsApp with the help of Intent in Android?

Solution

Try the following solutions

            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);

Additional information about the process of finding a solution:

After reverse engineering WhatsApp, I found the following Android list fragment

Normal share intent, using “SEND”, which does not allow you to send to a specific contact and requires a contact selector.

The specific contact is picked up by the Conversation class, using the “SEND_TO” Action, but using the text message body, you cannot pick up attachments such as pictures.

 <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 the decompiler of the ContactPicker and Conversation classes, it was found that the extra key value of the phone number was “jid“.

Related Problems and Solutions