如何使用SHOW_OR_CREATE_CONTACT,还可以设置为联系人的照片?还可以、设置为、如何使用、联系人

2023-09-06 07:31:09 作者:又酷又能打.

我知道有一些可以通过这个意图来设置各个领域,如地址,电话号码,姓名等......

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.

我试了下code,但它似乎并没有工作:

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?

我也试图找出如果添加成功,这样我就可以拿到接触键,可以修改它,如果我需要,但似乎startActivityForResult并不在这里帮助

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.

推荐答案

没有。

SHOW_OR_CREATE_CONTACT 意图启动 ShowOrCreateActivity 。而从文档:

从任何额外的 ContactsContract.Intents.Insert 类将一起到创建活动,如果没有接触,以显示传递。

什么时候用or,什么时候用and

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

所以 ContactsContract.Intents.Insert.POSTAL 应该工作,但我们实在没有理由 ContactsContract.CommonDataKinds.Photo 可能会奏效。

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

从code,通过电话联系,如果URI是电话的previous活动搜索:或电子邮件,如果它是至mailto:,如果没有被发现,提示用户和正面按钮开始createIntent。

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

其中 mCreateExtras 包含您传递给 SHOW_OR_CREATE_CONTACT 所有演员(见 的onCreate

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

按照AndroidManifest ,这将启动一个 ContactEditorActivity 它建立了一个工具栏,并将一 ContactEditorFragement

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

旅程已接近尾声。数据绑定在 bindEditorsForNewContact()并似乎有限到什么 RawContactModifier.parseExtras()提供。文档似乎是正确的。

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

然而,还有另一种方式插入一个接触,直接与内容提供商

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:

帮助用户通过启动SHOW_OR_CREATE_CONTACT创建联系人一旦接触已经建立,插入该联系人的显示图片。