How to use SHOW_OR_CREATE_CONTACT and also set a photo for the contact?

浪子不回头ぞ 提交于 2019-12-08 04:57:28

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!