2015-05-06 59 views
0

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

下面是相关代码:

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的输出(从动作条第一,第二,从通知)

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 
+0

您是否可以发布在两种情况下在logcat中打印的意图 - 何时有效以及何时不会?我的猜测是这个问题与指定目标应用程序有关。 putExtra()部分应该按原样正常工作。 – RocketRandom

+0

那些日志是你的意思吗? –

回答

0

你需要调用createChooser()前设置Intent.FLAG_GRANT_READ_URI_PERMISSIONshareIntent

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