Android 使用联系人ID设置联系人图片的步骤
我有一个位图和一个联系人id。我想要一个函数,该函数接受这些参数并将位图设置为该id的联系人图片。您能帮我吗?试试看 将位图转换为byteArrayAndroid 使用联系人ID设置联系人图片的步骤,android,android-contacts,Android,Android Contacts,我有一个位图和一个联系人id。我想要一个函数,该函数接受这些参数并将位图设置为该id的联系人图片。您能帮我吗?试试看 将位图转换为byteArray Bitmap bit; // <-- put your bitmap here ByteArrayOutputStream streamy = new ByteArrayOutputStream(); bit.compress(CompressFormat.PNG, 0, streamy); byte[] photo = streamy.
Bitmap bit; // <-- put your bitmap here
ByteArrayOutputStream streamy = new ByteArrayOutputStream();
bit.compress(CompressFormat.PNG, 0, streamy);
byte[] photo = streamy.toByteArray();
不要忘记在
清单文件中添加权限WRITE\u CONTACTS
和READ\u CONTACTS
,这是一种复制粘贴操作。我看过这篇文章。你在哪里使用过联系人ID?你没有在整个代码中看到yourContectID
?有关您的种类信息,请参见联系人ID
。这不是你在这里提问的方式。你不会雇佣来解决你的家庭作业。在这里发布问题之前,请努力学习,并编写一些代码。带着你们正面临的问题。
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " == " +
ContentUris.parseId(yourContectID) + " AND " + Data.MIMETYPE + "=='" +
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = managedQuery(
ContactsContract.Data.CONTENT_URI,
null,
where,
null,
null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if(cursor.moveToFirst()){
photoRow = cursor.getInt(idIdx);
}
cursor.close();
values.put(ContactsContract.Data.RAW_CONTACT_ID,
ContentUris.parseId(yourContectID));
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if(photoRow >= 0){
this.getContentResolver().update(
ContactsContract.Data.CONTENT_URI,
values,
ContactsContract.Data._ID + " = " + photoRow, null);
} else {
this.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI,
values);
}
}