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

在android N中打开PDF文件时,空白页显示黑色,android,pdf,android-pendingintent,Android,Pdf,Android Pendingintent,在我的项目中打开Pdf文件时,我面临一个问题 我用通知类型实现了PDF打开。打开下载的PDF文件时,会显示空白页 清单文件: <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermission

在我的项目中打开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> 
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));
Uri=FileProvider.getUriForFile(这是一个新文件(filePath));

您的
默认提供程序
将是-
+“.fileprovider”
,如您在上述清单中所述。

任何解决方案,请让我知道您选择哪个应用程序打开pdf?实际上,我正在使用Adope pdf reader应用程序打开pdf文件,现在除了Adope外,它工作正常,PDF文件未打开其他应用@Pratikbutanical实际上我正在使用Adope PDF reader应用打开PDF文件,现在它工作正常,除了Adope PDF文件未打开其他应用@Vishal Arora