无法从 intent.getParcelableExtra(Intent.EXTRA_STREAM) 获取捕获的屏幕截图的 URI!
Can't get URI of captured screenshot from intent.getParcelableExtra(Intent.EXTRA_STREAM)!
当我从 Open With
对话框中选择我的应用程序时,我正在尝试获取刚刚捕获的 screenshot
的 URI
。但在提供的代码示例中,我总是从 intent.getParcelableExtra(Intent.EXTRA_STREAM)
中得到 null。
这是我的intent filter
:
实施了两个 intent-filter
第一个:制作我的 activity 主程序和启动程序。
第二个:使其成为图像查看器。(在系统上将此activity注册为图像查看器)
<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>
这就是我试图从调用我的 activity 的意图中获取 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
}
}
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
}
}
引用 the documentation for ACTION_VIEW
:
Input: getData() is URI from which to retrieve data.
因此,将您的代码更改为:
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();
}
}
当我从 Open With
对话框中选择我的应用程序时,我正在尝试获取刚刚捕获的 screenshot
的 URI
。但在提供的代码示例中,我总是从 intent.getParcelableExtra(Intent.EXTRA_STREAM)
中得到 null。
这是我的intent filter
:
实施了两个 intent-filter
第一个:制作我的 activity 主程序和启动程序。
第二个:使其成为图像查看器。(在系统上将此activity注册为图像查看器)
<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>
这就是我试图从调用我的 activity 的意图中获取 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
}
}
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
}
}
引用 the documentation for ACTION_VIEW
:
Input: getData() is URI from which to retrieve data.
因此,将您的代码更改为:
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();
}
}