2

My intention is to send a PDF file via whatsapp through my application.What happening now is its not opening whatsapp appliation.Below is my code:

                Uri uri = Uri.fromFile(pdfFile);
                Intent share = new Intent(Intent.ACTION_SEND);
                //share.setAction(Intent.ACTION_SEND);
                share.setType("application/pdf");
                share.putExtra(Intent.EXTRA_STREAM, uri);
                share.setPackage("com.whatsapp");
                startActivity(Intent.createChooser(share,""));
Siva
  • 1,849
  • 2
  • 13
  • 28

1 Answers1

2

As per my understanding you don't need to create chooser, and I hope you want to directly share to WhatsApp.

Whatsapp Documentation :
If you prefer to share directly to WhatsApp and bypass the system picker, you can do so by using setPackage in your intent.

WhatsApp Documentation

 Uri uri = Uri.fromFile(pdfFile);
 Intent share = new Intent(Intent.ACTION_SEND);
 share.setType("application/pdf");
 share.putExtra(Intent.EXTRA_STREAM, uri);
 share.setPackage("com.whatsapp");
 startActivity(share);

Edit

For target Sdk 24 or above you need to use FileProvider class to be able to use specific file. follow directions which are listed here.

Nakul
  • 1,313
  • 19
  • 26