Intent过滤器:intent.getData()返回null [英] Intent filter: intent.getData() returns null

查看:1257
本文介绍了Intent过滤器:intent.getData()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android中编写图像编辑器。到目前为止它只有一个活动。通过此活动,我可以使用调用Gallery活动的菜单选项加载图像,在onActivityResult中,我将接收到的图像处理为画布,以便我可以编辑它。这很好。

I'm writing an image editor in Android. So far it has a single activity. From this activity, I can load an image using a menu option that calls the Gallery activity, and in onActivityResult I treat the received image to paint my canvas so I can edit it. This is working fine.

我现在要做的是让这个活动可以从其他任何地方调用,所以不必从活动内部选择一个图像,我可以打开图库,选择一张照片并将其发送到我的应用程序。但我无法弄清楚如何。

What I'd like to do now is making this activity callable from anywhere else, so instead of having to pick an image from inside the activity, I could just open the Gallery, pick a photo and send it to my app. But I can't figure out how.

在我的清单上,我将以下内容放在< activity> 标签:

On my manifest, I've put the following inside the <activity> tags:

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

然后,在我的活动中,如 http://developer.android.com/training/basics/intents/filters.html ,我把以下内容放入onCreate():

Then, in my activity, as mentioned in http://developer.android.com/training/basics/intents/filters.html , I've put the following inside onCreate():

...
//after initializing my view and stuff

Intent intent =getIntent();
Uri image = intent.getData();

//code to do something with my uri
...

当我在手机中安装应用程序然后打开图库时,我可以选择图像并使用发送菜单,我的应用程序将显示在应用程序列表中,但是当我选择它时,我的应用程序会打开但忽略图片。事实证明,我从 intent.getData()获得的uri为空,我无法弄清楚原因。

When I install the app in my phone and then open the Gallery, I can pick an image and using the "send" menu and my app will show up in the app list, but when I select it my app opens but ignores the image. It turns out that the uri I'm getting from intent.getData() is null, and I can't figure out why.

我做错了什么?我忘了什么吗?如果你必须知道,我使用随附的CoolIris画廊购买Android 2.3。

Am I doing anything wrong? Am I forgetting something? If you must know, I'm on stock Android 2.3 using the CoolIris gallery provided with it.

推荐答案

你想要的Uri是在捆绑中。我尝试了以下代码,可以获得图像的Uri
,例如 content:// media / external / images / media / 1750

The Uri you want is in the bundle. I have tried below code and can get the Uri of the image such as content://media/external/images/media/1750

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
Uri uri = (Uri)bundle.get(Intent.EXTRA_STREAM);

更多信息,你可以看到 ACTION_SEND

More information you can see the ACTION_SEND

这篇关于Intent过滤器:intent.getData()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆