title: Permission Denial when trying to send CSV file to email, requires the provider be exported, or grantUriPermission()
body: <p>I am generating a CSV file from data pulled from Firebase and then trying to attach it to an email (and about two weeks ago this code worked!), but I keep getting the same error of a toast message saving the file cannot be attached and the following error in my log cat when I perform this task now:</p>
  <pre><code>2020-08-20 17:05:43.355 11147-11268/com.example.cpd E/DatabaseUtils: Writing exception to parcel
  java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.example.cpd.provider/external/MyCPDAudit.csv from pid=30180, uid=1000 requires the provider be exported, or grantUriPermission()
  </code></pre>
  <p>My code to generate the email Intent is as follows</p>
  <pre><code>            //INTENT TO SEND USER EMAIL
              Intent emailIntent = new Intent(Intent.ACTION_SEND);
              emailIntent.setType(&quot;text/plain&quot;);
              //SETS THE USERS EMAIL AS THE RECIPIENT EMAIL ADDRESS
              emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{user.getEmail()});
              emailIntent.putExtra(Intent.EXTRA_SUBJECT, &quot;My Audit Profile&quot;);
              emailIntent.putExtra(Intent.EXTRA_TEXT, &quot;Thank you for using CPD Journal to build your CPD Audit Profile. Please see the attached file for your records.&quot;);
              emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  
              File file = new File(csv);
              Uri uri = FileProvider.getUriForFile(SavedAudit.this, &quot;com.example.cpd.provider&quot;, file);
              emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
  
              //USER CAN SELECT HOW THEY WANT TO SHARE THE CSV FILE
              startActivity(Intent.createChooser(emailIntent, &quot;Pick an email provider&quot;));
  </code></pre>
  <p>I have the following permissions in my Android Manifest</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  </code></pre>
  <p>File provider in Manifest is</p>
  <pre><code>    &lt;provider
          android:name=&quot;androidx.core.content.FileProvider&quot;
          android:authorities=&quot;com.example.cpd.provider&quot;
          android:exported=&quot;false&quot;
          android:grantUriPermissions=&quot;true&quot;&gt;
          &lt;meta-data
              android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;
              android:resource=&quot;@xml/file_paths&quot; /&gt;
      &lt;/provider&gt;
  </code></pre>
  <p>My file paths are</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;paths&gt;
      &lt;external-path
          name=&quot;external&quot;
          path=&quot;.&quot; /&gt;
      &lt;external-files-path
          name=&quot;external_files&quot;
          path=&quot;.&quot; /&gt;
      &lt;cache-path
          name=&quot;cache&quot;
          path=&quot;.&quot; /&gt;
      &lt;external-cache-path
          name=&quot;external_cache&quot;
          path=&quot;.&quot; /&gt;
      &lt;files-path
          name=&quot;files&quot;
          path=&quot;.&quot; /&gt;
  &lt;/paths&gt;
  </code></pre>
  <p>As per the logcat, I have tried exporting the provider in my Manifest file, but this crashes the app on launch.</p>
  <p>I have tried adding the following two lines with my email Intent, but this results in the same log cat error:</p>
  <pre><code>String packageName = getApplicationContext().getPackageName();
  grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
  </code></pre>
  <p>My csv and uri log as follows</p>
  <pre><code>2020-08-20 18:03:30.969 20914-20914/com.example.cpd D/TAG: csv = /storage/emulated/0/MyCPDAudit.csv
  2020-08-20 18:03:30.974 20914-20914/com.example.cpd D/TAG: uri = content://com.example.cpd.provider/external/MyCPDAudit.csv
  </code></pre>
  <p>However, I'm not sure if I'm using this grantUriPermission method correctly by calling it like this - any ideas how to solve this?</p>