-1

here is the code through which i am sharing my image or video from an adaptor to WhatsApp it was working fine but now just the toast is shown that whats app not install is there any issue in the code am I missing something?

public void shareWhatsapp(String type, String path, String package_name) {

        Uri uri = FileProvider.getUriForFile(mFragment, BuildConfig.APPLICATION_ID + ".provider", new File(path));

        PackageManager pm = mFragment.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(package_name, PackageManager.GET_META_DATA);
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            sharingIntent.setType(type);
            sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
            sharingIntent.setPackage(package_name);
            mFragment.startActivity(Intent.createChooser(sharingIntent, "Share via"));
        } catch (PackageManager.NameNotFoundException e) {
            Toast.makeText(mFragment, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                    .show();
        }
    }

here is the listener

   holder.repostWhatsapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ModelStatus curVideo = getItem(position);
                if (curVideo.getFull_path().endsWith(".jpg")) {
                    shareWhatsapp("image/jpg", curVideo.getFull_path(), "com.whatsapp");
                } else if (curVideo.getFull_path().endsWith(".mp4")) {
                    shareWhatsapp("video/mp4", curVideo.getFull_path(), "com.whatsapp");
                }
            }
        });

1 Answers1

1

use below code to share image or video to whatsapp

 holder.repostWhatsapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Uri uri = FileProvider.getUriForFile(PdfRendererActivity.this, 
              PdfRendererActivity.this.getPackageName() + ".provider", outputFile);
        
                 if (curVideo.getFull_path().endsWith(".jpg")) {
                            shareWhatsapp("image/jpg", uri);
                        } else if (curVideo.getFull_path().endsWith(".mp4")) {
                            shareWhatsapp("video/mp4", uri);
                        } 
                    }
                });

  

shareWhatsApp

void shareWhatsapp(String type, String uri)
{
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType(type);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");
startActivity(share);
}
Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31