Share text image on WhatsApp

Mauro Vacchio
OverApp
Published in
4 min readAug 5, 2022

--

By Mauro Vacchio, Android Mobile Developer at OverApp.

WhatsApp is one of the most popular messaging applications, therefore it is frequently required to be integrated into the apps we are going to work on, whether it is to start a video call, send preset messages or share images.

In the WhatsApp FAQ section it is very well explained how to use their API to send a text-only message directly to a predetermined number, or how to prepare an Intent in order to set up a message composed of text plus media, but in this case it will be necessary to select the recipient on WhatsApp before you can send it.

But is it possible to combine the two, and add a phone number on the app side to which you can directly send a message consisting, for example, of text and image?

In this article we will see how to do it, in the first part we will select a number from our phone book, in the second part we will select an image from the gallery and finally we will prepare the intent to send the message to the selected number.

To be able to select a phone number, you must enter the following authorization to read contacts in the manifest:

and make sure that the user has authorized the app to access the address book, to retrieve the phone number you must launch the following intent:

Once the result is obtained, as explained in the official documentation, you will have to use Cursors to retrieve the telephone number, in this specific case I used a first Cursor to retrieve the data of the selected contact (id) and, once verified that the contact a telephone number is associated, a second Cursor to retrieve it.

Note: to work correctly, the telephone number to which the message is to be sent must include the international prefix without the +, without spaces and without leading zeros, so if the number I recover from the address book will be +39 3xx xxx xxxx or 0039 3xx xxx xxxx, it will be modified by removing + and spaces, then 393xxxxxxxxx.

Selecting an image from the gallery is extremely easy, you launch an intent of any image type and action get content:

from the result we will extract the data field that will contain the URI of the image to attach to the Intent with which we will call WhatsApp.

Now that we have selected an image and a contact to send it to, we can prepare the Intent with which we carry out the action, first of all we need to check if WhatsApp is actually installed on the device we are using, to do this we create a method in which we try to search in the packageManager for the WhatsApp package (com.whatsapp): if the app is present, the search will be successful, it will return true, otherwise an exception caught in the catch branch will be raised, and false will be returned.

Once we have made sure that WhatsApp is actually installed we can prepare our Intent, to which we are going to add some extras: the first extra will be for the image, it will have the name Intent.EXTRA_STREAM, while its value will be the URI of the image we want to send , the second extra will have as name Intent.EXTRA_TEXT, and as value any text we want to send, if we want to send only an image this extra can be omitted, finally the last extra is for the recipient, it will have as name the string jid, while as a value the recipient’s telephone number, formatted as previously explained, to which the suffix “@ s.whatsapp.net” must be added.

To conclude, all we have left is to set the action of our Intent (Intent.ACTION_SEND), the default package (“com.whatsapp”) and the type (“image / *”).

It will be sufficient to start a new Activity with this intent, and since WhatsApp is already present in the device, it will be opened with the message already filled in and ready to be sent.

Thanks to Giampietro Fronteddu and Davide Fin for their advice.

--

--