How to receiving several vcard via intent-filter and action.SEND_MULTIPLE

Viewed 970

I try to receive more than one contact in my app from the share feature in the Android Contact App but i receive only the first contact, I never receive action with ACTION_SEND_MULTIPLE.

I have used https://developer.android.com/training/sharing/receive as inspiration.

AndroidManifest :

<activity android:name=".ShareActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/x-vcard" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND_MULTIPLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/x-vcard" />
    </intent-filter>
</activity>

ShareActivity:

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
    // My code always pass here because action is always equals to ACTION_SEND even with several contacts selectionned
    if ("text/x-vcard".equals(type)) {
        handleSendContact(intent);
    }
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
    if ("text/x-vcard".equals(type)) {
        handleSendMultiContact(intent);
    }
}

private void handleSendContact(Intent intent) {
    Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    // use uri to get lookupkey...
}

private void handleSendMultiContact(Intent intent) {
    ArrayList<Uri> contactUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    for (Uri uri : contactUris) {
        // use uri to get lookupkey...
    }
}

Sharing a single contact, all work perfectly, but when I select two or more contact my received action still is ACTION_SEND and not ACTION_SEND_MULTIPLE and I receive only the first selected contact. My goal is to receive all contact selectionned in the Android Contact App.

2 Answers

Short version :

ACTION_SEND_MULTIPLE is never used with Contacts/vCard sharing. Instead, ACTION_SEND is always used with data containing multiple contacts and the uri containing multi LOOKUP_KEY separated by :.


Long version:

Sharing contact one :

content://com.android.contacts/contacts/as_multi_vcard/2345r1817-373F294D4F4329413151472F294F312F

BEGIN:VCARD
VERSION:2.1
N:NAME_1;
TEL;WORK:039-999-9999
EMAIL;HOME:updated@home.fr
URL:www.home_updated.fr
END:VCARD

Sharing contact two :

content://com.android.contacts/contacts/as_multi_vcard/2345r1820-4D553943354B2F514F4D553943354B2F514F

BEGIN:VCARD
VERSION:2.1
N:NAME_2;
TEL;WORK:039-999-9999
EMAIL;HOME:updated@home.fr
URL:www.home_updated.fr
END:VCARD

Sharing contacts one + two :

content://com.android.contacts/contacts/as_multi_vcard/2345r1817-373F294D4F4329413151472F294F312F%3A2345r1820-4D553943354B2F514F4D553943354B2F514F

BEGIN:VCARD
VERSION:2.1
N:NAME_1;
TEL;WORK:039-999-9999
EMAIL;HOME:updated@home.fr
URL:www.home_updated.fr
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:NAME_2;
TEL;WORK:039-999-9999
EMAIL;HOME:updated@home.fr
URL:www.home_updated.fr
END:VCARD

As you can observe above, sharing both contacts results on an Uri with the two LOOKUP_KEY separated by %3A (colon in ASCII). I found the aosp code doing that in PeopleActivity.java for details.

So I can exploit multiple contact sharing with that chunk of code:

if (Intent.ACTION_SEND.equals(action) && type != null) {
    Log.d("type", "type: " + type);
    if (type.equals("text/x-vcard")) {
        handleSendContact(intent);
    }
}

void handleSendContact(Intent intent) {
    Uri contactUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (contactUri != null) {
        //  getLastPathSegment() decodes "%3A" to ":", so split must be done on colon
        String[] lookupKeys = contactUri.getLastPathSegment().split(":");
        for (String lookupKey : lookupKeys) {
            // Query contact with lookup key
        }
    }
}

A single VCard file can contain one or more contacts, so I'm assuming the sending app is creating one VCard file for all selected contacts, which explains why SEND is more appropriate then SEND_MULTIPLE.

Check if your handleSendContact falsely assumes the VCard contains only one contact.

You can check the VCard being sent by sharing it via Gmail app instead, and send the file to yourself, and inspect it via some text editor. It should contain all selected contacts as expected.

Related