Opening a file in the Android N download folder (API 24+)

All I wanted was to open the PDF file in my external download directory.

I used this code to open the file and it worked 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 resets the message android.os.FileUriExposedException

I followed the link - stack overflow.site/questions/22791 / ... 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 xml as provider -

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

and show how -

<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 application does not split into N, but pdf applications cannot open the file.
Any help in this direction?

+4
source share
1 answer

, , .

:

  • "application/" + getExtension(filename) MIME , MIME application/ (, image/jpeg, text/plain). MimeTypeMap.

  • setType() , Intent, Intent Uri.

, , , - , DIRECTORY_DOWNLOADS /. :

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
        + File.separator + filename);

:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fiename);

pdf

Intent Uri, , . setDataAndType(), Uri MIME. setData() setType(), FileProvider MimeTypeMap MIME.

FWIW, FileProvider PDF PDF .

+2

Source: https://habr.com/ru/post/1667073/


All Articles