How to send video directly to a WhatsApp number Android Studio?

74 Views Asked by At

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

2

There are 2 best solutions below

2
On

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

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

}