简体   繁体   中英

implicit intent: shating images from gallery to my app

I'm trying to make the gallery app share images to my app, the problem is that I don't know how to get the image data sent to me by the gallery. I assumed that I may found the data in the method .getData() but it returns null

this is my intent-filter

<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>

and here is my code in 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);
    }

if the method getData() is not what I want, then how can I get the image that meant to be shared with my app?

So, I think my answer is a little bit late, but anyhow, here it is:

You have already set the intent-filter in the AndroidManifest.xml correct like this:

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

As on this Website stated, you get the URI with this command:

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

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

Dokumentation about the Intent class can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM