All I wanted was to open a pdf file in my External Downloads Directory.
I used this code to open the file, and it used to work fine.
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));
    }
Now in android N(API 24+), it crashes saying android.os.FileUriExposedException
I followed the link - https://stackoverflow.com/a/38858040/4788557 and https://developer.android.com/training/secure-file-sharing/share-file.html#ShareFile to convert my code to this -
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));
    }
and added provider xml as -
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
and manifest as -
<manifest...>
....
<application...>
....
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
....
</application>
So, now the app doesn't crash on N, but the pdf applications are unable to open the file.
 Any help in this direction?
 
     
    