How to use SHOW_OR_CREATE_CONTACT and also set a p

2019-06-03 00:51发布

问题:

Background

I know that there are various fields that can be set via this intent, such as address, phone numbers, name, etc... :

final Intent intent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, Uri.parse("tel:" +PhoneNumberService.formatNumber(phoneNumber, PhoneNumberFormat.NATIONAL));
intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
intent.putExtra(ContactsContract.Intents.Insert.POSTAL_ISPRIMARY,address);
intent.putExtra(ContactsContract.Intents.Insert.POSTAL,address);
... //other stuff, like on this post: http://stackoverflow.com/q/3456319/878126

The problem

I'd like to know if it's possible to also add a photo.

What I've tried

I've tried the next code, but it doesn't seem to work:

public static byte[] toByteArray(final Bitmap bitmap) {
    if (bitmap == null || bitmap.isRecycled())
        return null;
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    final byte[] byteArray = stream.toByteArray();
    return byteArray;
}


final byte[] imageByteArray = toByteArray(mBitmap);
intent.putExtra(ContactsContract.CommonDataKinds.Photo.PHOTO, imageByteArray);

The question

Is it possible to put the photo too? If so, how?

I've also tried to figure out if the adding has succeeded, so that I could get the contact-key and be able to modify it if I will need to, but it seems that "startActivityForResult" doesn't help here.

回答1:

Short answer

No.

The SHOW_OR_CREATE_CONTACT intent starts the ShowOrCreateActivity. And from the documentation:

Any extras from the ContactsContract.Intents.Insert class will be passed along to the create activity if there are no contacts to show.

So ContactsContract.Intents.Insert.POSTAL should work, but there is really no reason why ContactsContract.CommonDataKinds.Photo might work.

Long Answer

From the code, the previous activity searches for the contact by phone if the Uri is tel: or email if it is mailto:, and if none is found, prompts the user and the positive button starts createIntent.

            final Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
            createIntent.putExtras(mCreateExtras);
            createIntent.setType(RawContacts.CONTENT_ITEM_TYPE);

where the mCreateExtras contains all extras you passed to SHOW_OR_CREATE_CONTACT (see onCreate)

According to the AndroidManifest, this starts a ContactEditorActivity which sets up a toolbar and places a ContactEditorFragement.

The journey is almost over. The data binding is done in bindEditorsForNewContact() and seems limited to what RawContactModifier.parseExtras() provides. The documentation seems correct.

Workaround

However, there is another way to insert a contact, directly with the content provider.

 ContentValues values = new ContentValues();
 values.put(RawContacts.ACCOUNT_TYPE, accountType);
 values.put(RawContacts.ACCOUNT_NAME, accountName);
 // This may work (not tested)
 values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, imageByteArray);
 Uri rawContactUri = getContentResolver().insert(ContactsContract.CONTENT_URI, values);
 long rawContactId = ContentUris.parseId(rawContactUri);

So what you could do is:

  1. Help the user to create a contact by starting the SHOW_OR_CREATE_CONTACT
  2. Once the contact has been created, insert a display photo for this contact.


回答2:

I would like to know if you found a working answer for this. I would have commented but i don't have enough points.I am also still looking for a good answer.

From the documentation, new contact inserts cannot be created explicitly. That means that a new contact data needs to be inserted using a URI and values(implicitly).

The only possible way is to create a new contact using the content provider and add all fields including default image. Then immediately after, use ACTION_EDIT to let user edit the fields.

Look also into Intents.ATTACH_IMAGE