2015-05-06 57 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()文檔中的剪輯數據。