简体   繁体   中英

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: 在此处输入图片说明

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

在此处输入图片说明

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

Please set intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

It will grant access of your file to other providers

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM