安卓套联系人照片与ContactsContract插入意图 [英] Android: set contact photo with ContactsContract insert intent
问题描述
我使用ContactsContract插入发送用户一个新建联系人故意一个新的联系人。在code我用的是:
I am using ContactsContract to insert a new contact by sending the user to a "New contact" intent. The code I am using is:
Intent i = new Intent(Intent.ACTION_INSERT);
i.setType(Contacts.CONTENT_TYPE);
i.putExtra(Insert.NAME, "Some Contact Name");
i.putExtra(Insert.EMAIL, "address@email.com");
i.putExtra(Insert.PHONE, "123-456-7890");
startActivity(i);
不过,我还需要通过某种方式以本地存储的照片通过(在res /绘),以显示在这个新接触的意图。我希望会有一个简单的方法来做到这一点,像
However, I need to also somehow pass in a locally stored photo (in res/drawable) to show up on this "New contact" intent. I was hoping that there would be an easy way to do this, like
i.putExtra(Insert.PHOTO, uri_to_photo);
但是这显然是行不通的。我发现这个线程详细介绍了如何将照片设置为已有的联系人(通过亚克的setPhoto()方法),但没有对如何在传递的照片,以显示为联系人图标上的新建联系人的意图。
but that obviously doesn't work. I found this thread detailing how to set the photo for an already-existing contact (via Jak's setPhoto() method), but nothing on how to pass a photo in to show up as the contact icon on the "New contact" intent.
什么是在传递一个照片(希望作为URI的照片),以新建联系人意图的最佳方式?
What would be the best way to pass a photo (hopefully as a URI to the photo) in to the "New contact" intent?
先谢谢了。
推荐答案
首先使用ContentProviderOperation的方式插入一个新的联系人。
Firstly use ContentProviderOperation's way to insert a new Contact.
final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
Bitmap bmp = YCardImageLoader.getInstance().getBitmapByCache(mTask.getImageUrl());
if (bmp != null ) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 100, stream);
byte[] bytes = stream.toByteArray();
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE)
.withValue(Photo.PHOTO, bytes)
.build());
}
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, mContact.getName())
.build());
ContentProviderResult[] result = SaveToPbkActivity.this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
然后得到结果URI作为ACTION_EDIT URI,把其他额外,startActivityForResult(意向,REQUEST_INSERT_CONTACT)。
Then get result uri as the ACTION_EDIT uri, put other extras, startActivityForResult(intent, REQUEST_INSERT_CONTACT).
Intent editIntent = new Intent(Intent.ACTION_EDIT);
uri = result[0].uri;
editIntent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
editIntent.putExtra("finishActivityOnSaveCompleted", true);
putExtras(editIntent, null);
startActivityForResult(editIntent, REQUEST_INSERT_CONTACT);
因为我们先插入,我们将删除它时,结果code!= RESULT_OK
because we insert first, we will delete it when resultCode != RESULT_OK
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_INSERT_CONTACT) {
if (resultCode == RESULT_OK) {
//SAVE SUCCESS
} else {
ContentResolver cr = getContentResolver();
cr.delete(uri, null, null);
}
} }
最后,对不起,我的英语!
At last sorry for my english!
这篇关于安卓套联系人照片与ContactsContract插入意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!