Getting Sending Failed Error while sending pdf through my app to whatsapp in android 11

Viewed 414

Sending pdf files from my app to whatsapp was working until it was used on android 11 device I have added this permission also and have asked for runtime permission

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R){
                if (isPermissonGranted()){
                    Log.i("storageproblem","app runtime permission granted");
                    Uri fileuri = Uri.parse("file://" + file);
                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.putExtra(Intent.EXTRA_STREAM, fileuri);
                    share.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    share.setPackage("com.whatsapp");
                    share.setType("*/*");
                    startActivity(share);
                }else{
                    Log.i("storageproblem","app runtime permission not granted");
                    takePermisson();
                }
            }
3 Answers

instead of

Uri fileUri = Uri.parse("file://" + file);

use

Uri fileUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.packagename.fileprovider", file);

Uri fileuri = Uri.parse("file://" + file);

Dont use a file uri but use a FileProvider to serve your file and can use a content scheme uri.

changing uri to file provider works

Related