Ishaan Kumar
Ishaan Kumar

Reputation: 985

Opening PDF file using Android intent

So here's the Scenario:

I am downloading a PDF from network and saving it in /0/Download/NSIT - Notices/"fileName".pdf

Now sending Intent like this:

private void openPDF(String fileName) {
    Log.d(TAG, "openPDF: called");
    Intent intent = new Intent(Intent.ACTION_VIEW);File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
    File myPDF = new File(Environment.getExternalStorageDirectory() + "/Download/NSIT - Notices", fileName + ".pdf");
    Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", myPDF);
    Log.d(TAG, "openPDF: intent with uri: " + uri);
    intent.setDataAndType(uri, "application/pdf");
    context.startActivity(Intent.createChooser(intent, "Open with..."));
}

Now any PDF reader (Google Drive PDF Reader, System PDF Viewer) is saying

The File Does Not Exist

Image: enter image description here

The Google one is sitting not doing anything (that's why not included its image)

Mainfest.xml

<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>

Provider_path.xml

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

I am creating File here in AsyncTask but opening from the postExecute of that AsyncTask

File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
            File myPDF = new File(myDir, params[1] + ".pdf");
            if (!myDir.exists())
                myDir.mkdirs();
            try {
                fileOutputStream = new FileOutputStream(myPDF);
                fileOutputStream.write(response.bodyAsBytes());
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "doInBackground: Download complete");

Logcat

enter image description here

Previously it was throwing FileUriExposedException then I fixed with this FileProvider thing. The problem is the pdf file downloaded by my app is created successfully and I am able to open it successfully from any File Manager (ES File Explorer now) but not from my app :(. I am new to FileProvider. Need help!

PS: That's not my device problem either. Tested with two other phones which has different ROMs and API >= 23

Upvotes: 4

Views: 8968

Answers (2)

Sunny
Sunny

Reputation: 14808

Please set intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

It will grant access of your file to other providers

Upvotes: 9

Serj
Serj

Reputation: 49

See documentation for File provider

Whether the content provider is available for other applications to use:

true: The provider is available to other applications. Any application can use the provider's content URI to access it, subject to the permissions specified for the provider.

false: The provider is not available to other applications. access to the provider to your applications. Only applications that have the same user ID (UID) as the provider will have access to it.

Try to change android:exported="true"

Upvotes: -1

Related Questions