Visakh
Visakh

Reputation: 259

FileProvider Showing blank in android Nougat

I have a pdf file in device's internal storage with location as "/storage/emulated/0/MyPdf/test.pdf". While accessing it with FileProvider, pdf reader is blank filled with black colour. path_file is as follows

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths>

java code snippet in my code is

File pdfFile = new File(Environment.getExternalStorageDirectory() + "/MyPdf/" + "test.pdf"); 
    Uri path  = FileProvider.getUriForFile(getContext(),"com.test.pdf.fileprovider", pdfFile);
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
    pdfIntent.setDataAndType(path, "application/pdf");
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    try{
        startActivity(pdfIntent);
    }catch(ActivityNotFoundException e){
        Toast.makeText(getContext(), "No Application available to view PDF", Toast.LENGTH_SHORT).show();
    }

provider in My manifest file

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.test.pdf.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

Please help me to overcome this issue

Upvotes: 0

Views: 833

Answers (2)

Anirudh Bhat
Anirudh Bhat

Reputation: 21

Shouldn't the second argument for .getUriForFile() must be authorities you have mentioned in manifest file?

"com.test.pdf.fileprovider" instead of ""com.visakh.horizon.horizon.fileprovider"

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007584

Replace:

pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

with:

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

as you should not need FLAG_ACTIVITY_CLEAR_TOP, but you definitely need FLAG_GRANT_READ_URI_PERMISSION.

Upvotes: 2

Related Questions