Why I am not able share pdf file through gmail?

Viewed 103

I have an app which has share pdf functionality. I have used fileprovider and intent to share pdf uri with other apps programatically on button click. MIME type is application/pdf

On Android 11 - Gmail version 2022.05.01.449051655 share pdf functionality is working fine where as Android os <11 i.e. on 10, 9 etc it is not able to attach pdf in mail draft. throwing "Unable to attach file." error message. (Gmail version is 2022.05.01.449051655)

I googled the issue, some says by allowing storage permission of gmail app it will start attaching file in mail. I have tried that as well but I am still not able to share pdf. (Tried it with latest Gmail version 2022.05.01.449051655)

While testing this issue I have used 3 different Gmail versions those are as follows: 2019.05.12.250526289.release 2019.11.21.283644823.release 2021.10.17.407218946.release With all these 3 older Gmail versions enabling the "Storage" permission manually solved our file sharing issue.

But with latest version of Gmail 2022.05.01.449051655 we are not able to attach file even though we manually allowed storage permission.

Seems like this is a bug in latest release of gmail android app. Please help us resolve this issue. This is a blocker for us.

Here is the code snippet

        Uri fileURI = FileProvider.getUriForFile(context, "com.fileprovider", mFile);

        Intent intent = ShareCompat.IntentBuilder.from(this)
                .setStream(fileURI) // uri from FileProvider
                .setType("application/pdf")
                .getIntent()
                .setAction(Intent.ACTION_SEND)
                .setDataAndType(fileURI, "application/pdf")
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivity(intent);
1 Answers

it's because you don't have sufficient permission to access the uri. You have to grant the required permissions when saving a file to storage like this.

Use the uri of a file you just saved to give the permissions.

grantUriPermission(getPackageName(),savedFileUri,Intent.FLAG_GRANT_READ_URI_PERMISSION);
Related