title: Opening a file in downloads folder Android N(API 24+)
body: <p>All I wanted was to open a pdf file in my External Downloads Directory.<br>
  <br>
  I used this code to open the file, and it used to work fine.</p>
  
  <pre><code>File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
              + File.separator + filename);
      Uri path = Uri.fromFile(file);
      Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
      pdfOpenintent.setDataAndType(path, "application/" + getExtension(filename));
      pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      try {
          CourseModulesActivity.this.startActivity(pdfOpenintent);
      } catch (ActivityNotFoundException e) {
          pdfOpenintent.setType("application/*");
          startActivity(Intent.createChooser(pdfOpenintent, "No Application found to open File - " + filename));
      }
  </code></pre>
  
  <p>Now in android N(API 24+), it crashes saying <code>android.os.FileUriExposedException</code><br></p>
  
  <p>I followed the link - <a href="https://stackoverflow.com/a/38858040/4788557">https://stackoverflow.com/a/38858040/4788557</a> and <a href="https://developer.android.com/training/secure-file-sharing/share-file.html#ShareFile" rel="nofollow noreferrer">https://developer.android.com/training/secure-file-sharing/share-file.html#ShareFile</a> to convert my code to this - </p>
  
  <pre><code>File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
              + File.separator + filename);
      Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
      Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
      pdfOpenintent.setData(path);
      pdfOpenintent.setType("application/" + getExtension(filename));
      pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      pdfOpenintent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      try {
         CourseModulesActivity.this.startActivity(pdfOpenintent);
      } catch (ActivityNotFoundException e) {
          pdfOpenintent.setType("application/*");
          startActivity(Intent.createChooser(pdfOpenintent, "No Application found to open File - " + filename));
      }
  </code></pre>
  
  <p>and added provider xml as - </p>
  
  <pre><code>&lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  &lt;external-path name="external_files" path="."/&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>and manifest as - </p>
  
  <pre><code>&lt;manifest...&gt;
  ....
  &lt;application...&gt;
  ....
      &lt;provider
          android:name="android.support.v4.content.FileProvider"
          android:authorities="${applicationId}.provider"
          android:exported="false"
          android:grantUriPermissions="true"&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></p>
  
  <p>So, now the app doesn't crash on N, but the pdf applications are unable to open the file.<br> Any help in this direction?</p>