2016-10-28 545 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