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

3

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

}

请告诉我任何解决方案。 - MurugananthamS
你选择哪个应用程序打开PDF文件? - Pratik Butani
实际上,我正在使用Adobe PDF阅读器应用程序打开PDF文件,现在它工作正常,除了Adobe之外,其他应用程序无法打开PDF文件。@PratikButani - MurugananthamS
请注意,您正在混合使用 intent.addFlagsintent.setFlags,后者将覆盖前者。 - Roland van der Linden
1个回答

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",就像您在上面的清单中提到的那样。

实际上,我正在使用Adobe PDF阅读器应用程序打开PDF文件,现在它运行良好,除了Adobe之外,其他应用程序无法打开PDF文件。@Vishal Arora - MurugananthamS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接