隐式意图:将图片从图库分享到我的应用程序

问题描述 投票:0回答:1

我试图让图库应用程序将图像共享到我的应用程序,问题是我不知道如何获取图库发送给我的图像数据。我假设我可以在 .getData() 方法中找到数据,但它返回 null

这是我的意图过滤器

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

这是我在 MainActivity 中的代码

Intent data = getIntent(); //get the intent that starts this activity, in this case: gallery intent
    if(data != null){ 
        Uri uri = data.getData();
        //set the image view: the problem is: uri is always null
        imageView.setImageURI(uri);
    }

如果 getData() 方法不是我想要的,那么我怎样才能获取要与我的应用程序共享的图像?

java android android-intent intentfilter android-implicit-intent
1个回答
0
投票

所以,我认为我的回答有点晚了,但无论如何,这是:

您已经在 AndroidManifest.xml 中正确设置了意图过滤器,如下所示:

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

此网站所述,您可以使用以下命令获取 URI:

Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

//set content of ImageView to the specific Uri
imageView.setImageURI(imageUri);

有关 Intent 类的文档可以在 here 找到。

© www.soinside.com 2019 - 2024. All rights reserved.