[英]Android: set contact photo with ContactsContract insert intent
I am using ContactsContract to insert a new contact by sending the user to a "New contact" intent. 我正在使用ContactsContract通过将用户发送到“新联系人”意图来插入新联系人。 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);
However, I need to also somehow pass in a locally stored photo (in res/drawable) to show up on this "New contact" intent. 但是,我还需要以某种方式传入本地存储的照片(以res / drawable格式)以显示此“新联系人”的意图。 I was hoping that there would be an easy way to do this, like
我希望会有一种简单的方法来做到这一点,例如
i.putExtra(Insert.PHOTO, uri_to_photo);
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.
我发现此线程详细说明了如何为已存在的联系人设置照片(通过Jak的setPhoto()方法),但没有任何有关如何传递照片以显示为“新联系人”意图上的联系人图标的内容。
What would be the best way to pass a photo (hopefully as a URI to the photo) in to the "New contact" intent? 将照片(希望作为照片的URI)传递到“新联系人”意图的最佳方法是什么?
Thanks in advance. 提前致谢。
Firstly use ContentProviderOperation's way to insert a new Contact. 首先使用ContentProviderOperation的方式插入新的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);
Then get result uri as the ACTION_EDIT uri, put other extras, startActivityForResult(intent, REQUEST_INSERT_CONTACT). 然后将结果uri作为ACTION_EDIT uri,放入其他附加内容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);
because we insert first, we will delete it when resultCode != RESULT_OK 因为我们先插入,所以当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! 最后为我的英语感到抱歉!
Try this code to add image with contact details using intent 尝试使用以下代码使用意图添加带有联系人详细信息的图像
var bit = BitmapFactory.decodeResource(getResources(), R.drawable.yourimagename); // your image
val data = ArrayList<ContentValues>();
var row = ContentValues();
row.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
val stream = ByteArrayOutputStream()
bit.compress(Bitmap.CompressFormat.PNG, 100, stream)
val byteArray = stream.toByteArray()
bit.recycle()
row.put(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArray);
data.add(row)
var intent = Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone1);
intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, phone2);
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
context.startActivity(intent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.