0

I want to select a video from the gallery and send it to a specific number through WhatsApp. I am able to select the video and open WhatsApp but it shows me to select the contact in WhatsApp. How can I send it directly without selecting a contact in WhatsApp? My currect code is:

 /*-------------------------------------------------------------------------------------*/

private void selectVideo(){

    Intent intent = new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Video"), REQUEST_TAKE_GALLERY_VIDEO);

}


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {

        if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {

            Uri uri  = data.getData();

            Intent share = new Intent(Intent.ACTION_SEND, uri);
            share.setPackage("com.whatsapp.w4b");
            share.setType("video/*");
            share.putExtra(Intent.EXTRA_STREAM, uri);
            share.putExtra(Intent.EXTRA_TEXT, "Hello");
            startActivity(Intent.createChooser(share,"Select Video"));

        }

    }

}

/*-------------------------------------------------------------------------------------*/

I want to send to a specif number like 8721XXXXXX

Bhaskar Jyoti Dutta
  • 1,740
  • 2
  • 15
  • 30

2 Answers2

0

Hi I know It's very late, but I hope this helps you

    // Create the intent and set the action
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    // set the type we want to send
    sharingIntent.setType("video/*");
    // set uri of the video that we want to send
    sharingIntent.putExtra(Intent.EXTRA_STREAM,  Uri.parse(YOUR_VIDEO_PATH));
    // add the profile lind that we want to send the vodeo to
    sharingIntent.putExtra("jid", NUNBER + "@s.whatsapp.net")
    // and at the end we set the package name of whatsapp;
    sharingIntent.setPackage(WHATSAPP_PACKGE_NAME); //com.whatsapp for example
    // add the neaded flags
    sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    // and we start the activity to open wahtsapp
    startActivity(sharingIntent);
Mr17
  • 1
  • 3
  • And NUMBER should contain the country code – Mr17 Jul 18 '23 at 06:44
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 23 '23 at 01:10
0

Here is the code snippet that i have been using in my app since last 5 years and this works flawlessly

 public void shareVideo(int position) {
    String sdCard = Environment.getExternalStorageDirectory().toString();
    VideoModel f = (VideoModel) recyclerViewItems.get(position);
    File sourcelocation = new File(sdCard + f.getDirectoryName() + "/" + f.getImgName());
    Uri imageUri = FileProvider.getUriForFile(
            requireActivity(),
            BuildConfig.APPLICATION_ID+".provider", sourcelocation);
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("*///*");
    requireActivity().startActivity(Intent.createChooser(shareIntent, "Share Using"));

}

Above code is for recyclerview you can tweak it according to your requirements. For example :-

  public void shareVideo() {
    Uri imageUri = FileProvider.getUriForFile(
            requireActivity(),
            BuildConfig.APPLICATION_ID+".provider", sourcelocation);
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("*///*");
    requireActivity().startActivity(Intent.createChooser(shareIntent, "Share Using"));

}
Siddharth Shakya
  • 350
  • 4
  • 11