在 android N 中打开 PDF 文件在空白页中显示黑色

Opening PDF file in android N shows black color in blank page

我在项目中打开 Pdf 文件时遇到问题。

我实现了通知类型的 PDF 打开。打开下载的 PDF 文件时显示空白页。

清单文件:

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

路径提供者:

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

Activity:

public void customNotifcation1() {
getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            File file = new File(getActivity().getFilesDir(), pdfFileName);
            String absoluteFilePath = file.getAbsolutePath();

            Uri uri = Uri.parse("content://" + 
  getActivity().getPackageName() + "/" + absoluteFilePath);
            PendingIntent pendingIntent = null;
            CharSequence name = "Attendance";
            String description = uri + "";

            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new 
  NotificationChannel("Attendance", name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = 
  getActivity().getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
            NotificationCompat.Builder notification = new 
  NotificationCompat.Builder(getActivity(), "Attendance")
                    .setContentTitle(name)
                    .setContentText(description)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.icon50);
            try {

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(uri, mimeType);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent = Intent.createChooser(intent, "Choose Pdf 
  Application");
                //pendingIntent = PendingIntent.getActivity(getActivity(), 
 0, intent, 0);
                pendingIntent = PendingIntent.getActivity(getActivity(), 0, 
  intent, PendingIntent.FLAG_CANCEL_CURRENT);

            } catch (ActivityNotFoundException e) {
                Toast.makeText(getActivity(), "No Application Available to 
   fiew this file type", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("Exception", e.toString());
            }


    notification.setLargeIcon(BitmapFactory.decodeResource(getResources(), 
  R.drawable.icon));
            notification.setContentIntent(pendingIntent);
            notificationManager.notify(1, notification.build());
        } else {
            PendingIntent pendingIntent = null;
            File file = null;
            NotificationCompat.Builder builder = new 
   NotificationCompat.Builder(getActivity());
             builder.setSmallIcon(R.drawable.icon50);

            try {
                file = new 
       File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + pdfFileName);
                file = new File(getActivity().getFilesDir(), pdfFileName);
                String absoluteFilePath = file.getAbsolutePath();

                Uri uri = Uri.parse("content://" + 
       getActivity().getPackageName() + "/" + absoluteFilePath);

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(uri, "application/pdf");
                // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // intent.setDataAndType(Uri.fromFile(file), 
  "application/pdf");
                // intent = Intent.createChooser(intent, "Open File");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                pendingIntent = PendingIntent.getActivity(getActivity(), 0, 
  intent, 0);

            } catch (ActivityNotFoundException e) {
                Toast.makeText(getActivity(), "No Application Available to 
  fiew this file type", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("Exception", e.toString());
            }

            builder.setContentIntent(pendingIntent);

      builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), 
 R.drawable.icon));
            builder.setContentTitle("Attendance");
            builder.setContentText(file + "");
            builder.setAutoCancel(true);

            NotificationManager notificationManager = (NotificationManager) 
 getActivity().getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(1, builder.build());
        }
    }
});

}

您需要 FileProvider 为您要打开的文件生成 Uri。

删除这个 -

Uri uri = Uri.parse("content://" + 
   getActivity().getPackageName() + "/" + absoluteFilePath);

并使用 -

Uri uri = FileProvider.getUriForFile(this, <DEFAULT-PROVIDER>, new File(filePath));

您的 DEFAULT-PROVIDER 将是 - <APPLICATION_ID> + ".fileprovider" 正如您在上面的清单中提到的那样。