title: Android 10 - file provider - permission denial: reading android.support.v4.content.FileProvider uri
body: <p>I've tried to generate a CSV file (of Accelerometer), save it to my <code>Context.getFilesDir()</code>, and then use FileProvider to share it to Google Drive.</p>
  
  <p>The problem is when I try to share it, error <code>Writing exception to parcel</code>, <code>java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://...csv requires the provider be exported, or grantUriPermission()</code> keeps occurring. </p>
  
  <p>I had... </p>
  
  <ol>
  <li>Added <code>grantUriPermission()</code> and <code>Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)</code> as others suggests.</li>
  <li>I tried to write to <code>Environment.getExternalStorageDirectory()</code>, but it doesn't work since my phone doesn't have external storage.</li>
  <li>I can not export the provider because it raises an error as well.</li>
  </ol>
  
  <p>What can I do then? Or is that a better way to generate and share a CSV file? Any suggestions would be grateful. Thank you, guys.</p>
  
  <p>Code Snippet for now</p>
  
  <p>In AndroidManifest.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>    &lt;application...&gt;
          ...
  
          &lt;provider
              android:name="android.support.v4.content.FileProvider"
              android:grantUriPermissions="true"
              android:authorities="com.example.FileProvider"
              android:exported="false"&gt;
              &lt;meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/provider_paths"/&gt;
          &lt;/provider&gt;
  
      &lt;/application&gt;
  </code></pre>
  
  <p>In provider_path.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;files-path name="files" path="."/&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>In mainActivity.java</p>
  
  <pre class="lang-java prettyprint-override"><code>    private void onShareCSV() {
  
          Uri path = FileProvider.getUriForFile(this, "com.example.FileProvider", new File(filePath + fileName));
  
          Log.d(TAG, "onShareCSV: " + path);
          Intent shareIntent = new Intent("com.example.FileProvider").setData(path);
  
          List&lt;ResolveInfo&gt; resInfoList = getPackageManager().queryIntentActivities(shareIntent, PackageManager.MATCH_DEFAULT_ONLY);
          for (ResolveInfo resolveInfo : resInfoList) {
              String packageName = resolveInfo.activityInfo.packageName;
              getApplicationContext().grantUriPermission(packageName, path, Intent.FLAG_GRANT_READ_URI_PERMISSION);
          }
  
          shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
          shareIntent.setAction(Intent.ACTION_SEND);
          shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a CSV I'm sharing.");
          shareIntent.putExtra(Intent.EXTRA_STREAM, path);
          shareIntent.setType("text/csv");
          startActivity(Intent.createChooser(shareIntent, "Share..."));
          this.setResult(RESULT_OK, shareIntent);
      }
  </code></pre>
  
  <p>And here is the error</p>
  
  <pre><code>2020-02-20 23:45:02.751 21484-21503/com.example.charleschung.mci_pa1 E/DatabaseUtils: Writing exception to parcel
      java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://com.example.FileProvider/files/sensor-1582260302226.csv from pid=21581, uid=1000 requires the provider be exported, or grantUriPermission()
          at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729)
          at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602)
          at android.content.ContentProvider$Transport.query(ContentProvider.java:231)
          at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:104)
          at android.os.Binder.execTransactInternal(Binder.java:1021)
          at android.os.Binder.execTransact(Binder.java:994)
  </code></pre>