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

时间:2019-01-31 04:56:30

标签: android pdf android-pendingintent

在我的项目中打开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());
        }
    }
});

}

1 个答案:

答案 0 :(得分:1)

您需要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",如您在上面的清单中所提到的。