PendingIntent不发送Extra

时间:2015-05-06 10:14:48

标签: android notifications android-pendingintent

我正在开发一个应用程序,您可以在Android设备上创建桌面的屏幕截图,虽然这很好用,但我在通知时分享图像时遇到了问题。我使用几乎相同的代码从AppBar共享和从通知共享,但通知操作不起作用。虽然它打开应用程序选择器应该如何,它不会发送带有它的图像。

以下是相关代码:

Uri lastShotUri; //populated from the MediaScanner

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, lastShotUri);
shareIntent.setType("image/png");
Intent intent = Intent.createChooser(shareIntent, getResources().getText(R.string.send_to));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
    0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.addAction(R.drawable.ic_share_variant_black_24dp, "Share", pendingIntent);

我不明白为什么它不在这里工作,但为什么它是从应用程序本身工作。如果有人知道我做错了什么,我真的很感激这方面的帮助。

Logcat输出(ActionBar中的第一个,来自Notification的第二个)

05-06 13:06:08.151     753-1373/? I/ActivityManager﹕ START u0 {act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras)} from uid 10198 on display 0
05-06 13:06:15.467     753-7488/? I/ActivityManager﹕ START u0 {act=android.intent.action.SEND typ=image/png flg=0xb080001 cmp=com.pushbullet.android/.ui.FloatingComposePushActivity (has clip) (has extras)} from uid 10198 on display 0
05-06 13:06:21.856     753-1706/? I/ActivityManager﹕ START u0 {act=android.intent.action.CHOOSER flg=0x10000000 cmp=android/com.android.internal.app.ChooserActivity bnds=[216,1227][1056,1371] (has extras)} from uid 10198 on display 0
05-06 13:06:24.544      753-769/? I/ActivityManager﹕ START u0 {act=android.intent.action.SEND typ=image/png flg=0xb080000 cmp=com.pushbullet.android/.ui.FloatingComposePushActivity (has extras)} from uid 10198 on display 0

1 个答案:

答案 0 :(得分:0)

在致电Intent.FLAG_GRANT_READ_URI_PERMISSION之前,您需要在shareIntent上设置createChooser()

两种情况下发送的Intent都存在一些差异。在“工作”情况下,设置标志Intent.FLAG_GRANT_READ_URI_PERMISSION。这为目标Activity提供了读取URI的权限。在日志中,您还可以看到文本“(有剪辑)”。请阅读Intent.createChooser()文档中的剪辑数据。