如何从通知中打开pdf

时间:2019-08-10 14:57:37

标签: android-intent android-notifications android-pendingintent

当我按通知时我想打开pdf 我试图做到这一点:

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);
    }
}

但是我得到这个错误:

--------- beginning of crash

E / Android运行时:致命异常:主要     进程:it.scout.hawkscout,PID:6611     android.os.FileUriExposedException:file:///storage/emulated/0/Download/10-08-2019%20sf%20fefe.pdf通过Intent.getData()在应用程序之外公开         在android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)         在android.net.Uri.checkFileUriExposed(Uri.java:2346)         在android.content.Intent.prepareToLeaveProcess(Intent.java:8965)         在android.content.Intent.prepareToLeaveProcess(Intent.java:8926)         在android.app.PendingIntent.getActivity(PendingIntent.java:340)         在android.app.PendingIntent.getActivity(PendingIntent.java:302)         在it.scout.hawkscout.view.PdfCreatorActivity.addNotification(PdfCreatorActivity.java:67)         在it.scout.hawkscout.listener.ListViewListener $ 1.onClick(ListViewListener.java:60)         在com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:166)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:154)         在android.app.ActivityThread.main(ActivityThread.java:6119)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 应用已终止。

文件路径中的“%”是否可能导致崩溃?

0 个答案:

没有答案