问题
I want to open pdf when i press on the notification i tried to to this:
public void addNotification(String path) {
    createNotificationChannel();
    File file = new File(path); // set your audio path
    Intent intent = new Intent();
    intent.setData(Uri.fromFile(file));
    intent.setAction(android.content.Intent.ACTION_VIEW);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    //Intent landingIntent = new Intent(this, MainActivity.class);
   // landingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
   // PendingIntent pendingIntent= PendingIntent.getActivity(this, 0,landingIntent, PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "0");
    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setContentTitle(nomePdf + " creato");
    builder.setContentText("Il tuo pdf è stato salvato nella cartella download");
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setContentIntent(pIntent);
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(0, builder.build());
}
public void createNotificationChannel(){
    CharSequence name = "Personal";
    String description = "";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel("0", name, importance);
        notificationChannel.setDescription(description);
        NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}
but i get this error:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main Process: it.scout.hawkscout, PID: 6611 android.os.FileUriExposedException: file:///storage/emulated/0/Download/10-08-2019%20sf%20fefe.pdf exposed beyond app through Intent.getData() at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799) at android.net.Uri.checkFileUriExposed(Uri.java:2346) at android.content.Intent.prepareToLeaveProcess(Intent.java:8965) at android.content.Intent.prepareToLeaveProcess(Intent.java:8926) at android.app.PendingIntent.getActivity(PendingIntent.java:340) at android.app.PendingIntent.getActivity(PendingIntent.java:302) at it.scout.hawkscout.view.PdfCreatorActivity.addNotification(PdfCreatorActivity.java:67) at it.scout.hawkscout.listener.ListViewListener$1.onClick(ListViewListener.java:60) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Application terminated.
Can be the "%" in filepath to cause crash?
来源:https://stackoverflow.com/questions/57443219/how-to-open-pdf-from-a-notification