如何使用SHOW_OR_CREATE_CONTACT,还可以设置为联系人的照片? [英] How to use SHOW_OR_CREATE_CONTACT and also set a photo for the contact?
问题描述
我知道有一些可以通过这个意图来设置各个领域,如地址,电话号码,姓名等......
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
意图启动<一个href=\"https://github.com/android/platform_packages_apps_contacts/blob/6b73f40ae214d66111e5b4cdbe58fd16afef0fdc/src/com/android/contacts/activities/ShowOrCreateActivity.java\"相对=nofollow> ShowOrCreateActivity
。
而从<一个href=\"http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#SHOW_OR_CREATE_CONTACT\"相对=nofollow>文档:
从任何额外的 ContactsContract.Intents.Insert
类将一起到创建活动,如果没有接触,以显示传递。
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:
,如果没有被发现,<一个href=\"https://github.com/android/platform_packages_apps_contacts/blob/6b73f40ae214d66111e5b4cdbe58fd16afef0fdc/src/com/android/contacts/activities/ShowOrCreateActivity.java#L207\"相对=nofollow>提示用户和正面按钮开始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
所有演员(见<一href=\"https://github.com/android/platform_packages_apps_contacts/blob/6b73f40ae214d66111e5b4cdbe58fd16afef0fdc/src/com/android/contacts/activities/ShowOrCreateActivity.java#L110\"相对=nofollow> 的onCreate
)
where the mCreateExtras
contains all extras you passed to SHOW_OR_CREATE_CONTACT
(see onCreate
)
<一个href=\"https://github.com/android/platform_packages_apps_contacts/blob/6b73f40ae214d66111e5b4cdbe58fd16afef0fdc/AndroidManifest.xml#L345\"相对=nofollow>按照AndroidManifest ,这将启动一个 ContactEditorActivity
它建立了一个工具栏,并将一<一href=\"https://github.com/android/platform_packages_apps_contacts/blob/6b73f40ae214d66111e5b4cdbe58fd16afef0fdc/src/com/android/contacts/editor/ContactEditorFragment.java\"相对=nofollow> ContactEditorFragement
。
According to the AndroidManifest, this starts a ContactEditorActivity
which sets up a toolbar and places a ContactEditorFragement
.
旅程已接近尾声。数据绑定在<一个完成href=\"https://github.com/android/platform_packages_apps_contacts/blob/6b73f40ae214d66111e5b4cdbe58fd16afef0fdc/src/com/android/contacts/editor/ContactEditorFragment.java#L769\"相对=nofollow> bindEditorsForNewContact()并似乎有限到什么<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/4.2_r1/com/android/contacts/model/RawContactModifier.java#526\"相对=nofollow> 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创建联系人
- 一旦接触已经建立,插入该联系人的显示图片。
这篇关于如何使用SHOW_OR_CREATE_CONTACT,还可以设置为联系人的照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!