简体   繁体   中英

Intent to open PDF file in Android N

I have been trying to open a PDF file using the intent. It works fine for devices prior to Adroid N. Following is the code I have used

File file = new File(gridItems.get(position).getPath());
                        Intent intent = null;
                        if (Build.VERSION.SDK_INT < 24) {
                            intent = new Intent(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        } else {
                            intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            Uri pdfURI = FileProvider.getUriForFile(GalleryPdfActivity.this, getApplicationContext()
                                    .getPackageName
                                            () +
                                    ".provider", file);
                            intent.putExtra(Intent.EXTRA_STREAM, pdfURI);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                            intent.setType("application/pdf");
                        }
                        try {
                            if (intent.resolveActivity(getPackageManager()) != null)
                                startActivity(intent);
                            else
                                AppUtils.toast("No Application found to open the pdf", GalleryPdfActivity.this);
                        } catch (Exception e) {
                            AppUtils.toast(e.getMessage(), GalleryPdfActivity.this);
                        }

The file chooser opens and I have selected Google PDF Viewer to open the app. But it returns an error "Cannot display PDF(no file received)" . I was able to open the same file in devices prior to Android N

Add FLAG_GRANT_READ_URI_PERMISSION on the Intent in your FileProvider case. Otherwise, the other app has no access to the content. See the documentation .

Add FLAG_GRANT_READ_URI_PERMISSION

Intent intent = new Intent(Intent.ACTION_VIEW)
Uri outputFileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file); 
intent.setDataAndType(outputFileUri, "application/pdf"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Intent in = Intent.createChooser(intent, "Open File");
startActivity(in);

also add provider_paths.xml at res -> xml folder and need to add below code at manifests

<application>
   <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                tools:replace="android:resource"
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
 </application>

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