2016-10-28 101 views
0

我試圖從Open With對話框中選取剛截取的screenshotURI。但是在提供的代碼示例中,我總是從intent.getParcelableExtra(Intent.EXTRA_STREAM)中得到空值。無法從intent.getParcelableExtra(Intent.EXTRA_STREAM)獲取捕獲的屏幕截圖的URI!

這是我intent filter

實現了兩個intent-filter小號

第一個:讓我的主要活動和啓動。

第二個:使它的圖像瀏覽器(寄存器在系統上本次活動作爲一個圖像瀏覽器)

<intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 
    <category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <data android:mimeType="image/*" /> 
</intent-filter> 

這就是我怎樣,我試圖從調用的意圖得到URI我。活動。

Intent intent = getIntent(); 
String action = intent.getAction(); 
String type = intent.getType(); 

if (Intent.ACTION_VIEW.equals(action) && type != null) { 
    if (type.startsWith("image/")) { 
     Uri mediaUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); 
     // Here mediaUri is always null 
    } 
} 
+1

URI值將在數據領域,你需要使用intent.getData()。 – Baba

回答

1

the documentation for ACTION_VIEW引用:

輸入:的getData()是URI從其中檢索數據。

因此,改變你的代碼:

Intent intent = getIntent(); 
String action = intent.getAction(); 
String type = intent.getType(); 

if (Intent.ACTION_VIEW.equals(action) && type != null) { 
    if (type.startsWith("image/")) { 
     Uri mediaUri = intent.getData(); 
    } 
} 
1
Intent intent = getIntent(); 
String action = intent.getAction(); 
String type = intent.getType(); 

if (Intent.ACTION_VIEW.equals(action) && type != null) { 
    if (type.startsWith("image/")) { 
     Uri mediaUri = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM); 
     // Here mediaUri is always null 
    } 
} 
+0

編譯器說:「將'intent.getParcelableExtra(Intent.EXTRA_STREAM)'強制轉換爲'URI'是多餘的」。我之前嘗試過沒有運氣。 – Eftekhari