Sending an image by WhatsApp

3

I want to send an image by whatsapp to a specific phone number without having to access the image gallery and within the contact's chat.

Now what I do is the following:

    Intent intent = new intent();
    intent.setaction(Intent.ACTION_SEND);

    intent.setPackage("com.whatsapp");

    intent.setData(Uri.Parse("https://api.whatsapp.com/send?phone=+333333"))
    intent.setType("image/*");

    intent.putExtra(Intent.EXTRA_STREAM,Uri.parse("/storange/emulate/0/imagen.png"));

    startActivity(intent);

with this code I achieve almost everything. The image is loaded but it leaves me out of the chat. I have to choose the user. It does not read the intent.Setdata and does not enter the contact's chat. (that is, it leaves me out of the chat and I need you to enter to just press the send button and that's it). I hope you can help me

    
asked by YANINA 20.06.2017 в 09:13
source

1 answer

2

The code that you sample could not compile, since it has several errors. Instead of using the path " /storange/emulate/0/ ", use Environment.getExternalStorageDirectory() with which you will get the path of the external directory.

I suggest this method

 private void sendImageWhatsApp(String phoneNumber, String nombreImagen) {
    try {
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory() + "/" + nombreImagen));
        intent.putExtra("jid", phoneNumber + "@s.whatsapp.net"); //numero telefonico sin prefijo "+"!
        intent.setPackage("com.whatsapp");
        startActivity(intent);
     } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getApplicationContext(), "Whatsapp no esta instalado.", Toast.LENGTH_LONG).show();
    }
}

The method receives the phone number you have registered, without the prefix "+" (this is very important) and the name of the image.

Example:

 sendImageWhatsApp("333333", "imagen.png");

For security reasons, the WhatsApp API does not allow sending the message automatically , it only shows it and it would be ready for the user to add more information or to interact by clicking on the "send" button:

    
answered by 20.06.2017 / 19:24
source