3

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 Answers3

2

instead of

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

use

Uri fileUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.packagename.fileprovider", file);
Nolequen
  • 3,032
  • 6
  • 36
  • 55
1

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.

blackapps
  • 8,011
  • 2
  • 11
  • 25
0

changing uri to file provider works

A.M
  • 1
  • 2
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/30702554) – Hussien Fahmy Jan 04 '22 at 16:38