SecurityException- Send email-attachement with FileProvider

34 Views Asked by At

I try of add attachement (a pdf in Directory_Download ) in a email. I use FileProvide but its dont work. I watch several topic with solution on web/stackoverflow but nothing. I have exception SecurityException: Permission Denial

When i test, i'have the menu (gmail, outlouk...) - i choose gmail . To open i see attachement for a few seconds and is delete next. To finish a popup informe : cannot add the file.

What is missing? - several day, i seach Ty Help me

my code :

 Intent intent=new Intent(Intent.ACTION_SEND);
            intent.setType("application/pdf");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            //intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            File file = new File(Environment.DIRECTORY_DOWNLOADS, "download.pdf");
            Uri uri = FileProvider.getUriForFile(this, "com.myprojet.myapplication.provider", file); // ${applicationId}.provider"   - BuildConfig.APPLICATION_ID +.provider
            intent.putExtra(Intent.EXTRA_STREAM, uri);

            intent.putExtra(android.content.Intent.EXTRA_EMAIL, "[email protected]");
            intent.putExtra(android.content.Intent.EXTRA_TEXT, "message");
            startActivity(Intent.createChooser(intent, "Sending email..."));

In Manifest :

<provider
            android:authorities="${applicationId}.provider"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/paths" />

I add permission:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

And my path file :

`<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="Download/" />
</paths>
0

There are 0 best solutions below