【问题标题】:How to open image from push notification click?如何从推送通知点击打开图像?
【发布时间】:2019-08-19 12:17:49
【问题描述】:

在我的应用程序中,我可以选择捕获屏幕截图并将其保存在自定义目录下方(例如 /storage/emulated/0/MyApp/screenshots/image.jpeg)。

所以,我想应用Pending Intent,用户可以点击通知,屏幕截图将在某些默认查看器上打开......

问题是:如何配置待定意图,以便用户单击通知图像后将在某些默认查看器中打开?

例如:我有一部 Pixel 手机,有截图的选项,截图准备好后我可以点击打开图片...

【问题讨论】:

    标签: android push-notification notifications android-notifications


    【解决方案1】:

    终于找到了这篇文章

    https://medium.com/@ali.muzaffar/what-is-android-os-fileuriexposedexception-and-what-you-can-do-about-it-70b9eb17c6d0

    我得到了什么

    清单

    <manifest ...>
    <application ...>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
    </manifest>
    

    创建 XML 文件res/xml/provider_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    </paths>
    

    代码更改

    Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
    
        File file = new File(iImagePath); // set your image path
    
        Uri uri = FileProvider.getUriForFile(iC, iC.getApplicationContext().getPackageName() + ".provider", file);
        intent.setDataAndType(uri, "image/*");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
        PendingIntent pIntent = PendingIntent.getActivity(iC, 0, intent, 0);
    
        Bitmap bm = BitmapFactory.decodeFile(iImagePath);
    
        NotificationCompat.Builder builder = mScreenshotBuilder.setContentTitle(iC.getString(R.string.screenshot_saved))//
                                                               .setContentText(iC.getString(R.string.tap_to_view_your_screenshot))//
                                                               .setContentIntent(pIntent);
    

    【讨论】:

      【解决方案2】:
       Intent intent = new Intent();
          intent.setAction(android.content.Intent.ACTION_VIEW);
          File file = new File("YOUR_IMAGE"); // set your image path 
          intent.setDataAndType(Uri.fromFile(file), "image/*");
      
          PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
      
          Notification noti = new NotificationCompat.Builder(this)
                  .setContentTitle("Screenshot")
                  .setContentText("Image Name")
                  .setSmallIcon(R.drawable.ic_launcher)
                  .setContentIntent(pIntent).build();
      
          noti.flags |= Notification.FLAG_AUTO_CANCEL;
      
          NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          notificationManager.notify(0, noti);
      

      【讨论】:

      • 我得到了这样的异常android.os.FileUriExposedException: file:///storage/emulated/0/MyApp/screenshots/image.jpeg exposed beyond app through Intent.getData()
      • file:/// 替换为 file://
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      相关资源
      最近更新 更多