java Android - 更新联系人
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9907751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Update a contact
提问by Manitoba
I'm trying to update a contact of my phone book directly from my app. I'm able to add and delete the contacts but the update just does nothing!
我正在尝试直接从我的应用程序更新我的电话簿的联系人。我可以添加和删除联系人,但更新什么也没做!
After the insert or when I grab the contacts, I collect the CONTACT_ID (Aka _ID).
插入后或抓取联系人时,我会收集 CONTACT_ID(又名 _ID)。
Here is my code for the update:
这是我的更新代码:
public void update(Relation r)
{
Log.e("", ""+r.getBook_id());
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Builder builder = ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_TYPE, null);
builder.withValue(RawContacts.ACCOUNT_NAME, null);
ops.add(builder.build());
// Name
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data._ID, new String[]{String.valueOf(r.getBook_id())});
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, r.getFirstName()+ " " +r.getLastName());
ops.add(builder.build());
// Number
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data._ID, new String[]{String.valueOf(r.getBook_id())});
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, r.getNumber());
builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
ops.add(builder.build());
// Picture
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(r.getPhoto()));
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data._ID, new String[]{String.valueOf(r.getBook_id())});
builder.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());
}
catch (Exception e)
{
e.printStackTrace();
}
// Update
try
{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
}
r.getBook_id()
gives me the _ID value of the current contact.
r.getFirstName()
and r.getLastName()
give me the name of the contact.
r.getPhoto()
gives me his photo uri.
And finally r.getNumber()
gives me his phone number.
r.getBook_id()
给我当前联系人的 _ID 值。
r.getFirstName()
并r.getLastName()
告诉我联系人的姓名。
r.getPhoto()
给我他的照片uri。最后r.getNumber()
给了我他的电话号码。
The Log line gives me a correct value of _ID, I don't think the problem comes from here. Did I do something wrong?
Log 行给了我一个正确的 _ID 值,我认为问题不是来自这里。我做错什么了吗?
Thanks.
谢谢。
Regards.
问候。
V.
五、
Whole source code:
整个源代码:
/**
* Uses the Contacts API to load contacts from the phonebook.
*
*/
public class ContactUtils
{
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.CONTACT_PRESENCE,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
private Activity context;
static final int CARD_LIMIT = 50;
/**
* Constructor
*
* @version 1.0
* @since 2012-03-28
* @param context Context of the running activity
*/
public ContactUtils(Activity context)
{
super();
this.context = context;
}
/**
* Returns a list of all the contacts in the phonebook
*
* @version 1.0
* @since 2012-03-28
* @return Returns a list of all the contacts in the phonebook
*/
public ArrayList<Relation> loadContacts()
{
String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND (" + ContactsContract.Contacts.HAS_PHONE_NUMBER + " == 1))";
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
context.startManagingCursor(c);
ArrayList<Relation> cList = new ArrayList<Relation>();
int a = 0;
Relation relation;
while (c.moveToNext() && a<CARD_LIMIT)
{
relation = new Relation();
relation.setBook_id(new Integer(c.getString(0)).intValue());
String[] name_splitted = c.getString(1).split(" ");
relation.setFirstName(name_splitted[0]);
if (name_splitted.length > 1) relation.setLastName(name_splitted[1]);
ArrayList<String> numbers = getPhoneNumbers(c.getString(0));
relation.setNumber(numbers.size() > 0 ? numbers.get(0) : "" );
relation.setConcept("Any");
relation.setPhoto(loadContactPhoto(c) == null ? "android.resource://com.orange.rd.kramer/drawable/default_photo" : loadContactPhoto(c));
cList.add(relation);
a++;
}
c.close();
return cList;
}
/**
* Returns an uri pointing to the contact's photo
*
* @version 1.0
* @since 2012-03-28
* @param cursor Cursor on a specific contact
* @return Returns an uri pointing to the contact's photo
*/
private String loadContactPhoto(Cursor cursor)
{
if (cursor.getString(5) != null)
{
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, cursor.getInt(0));
return Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY).toString();
}
return null;
}
/**
* Returns a list of all phone numbers that belong to the given contact
*
* @version 1.0
* @since 2012-03-28
* @param id Id of the given contact
* @return Returns a list of all phone numbers that belong to the given contact
*/
private ArrayList<String> getPhoneNumbers(String id)
{
ArrayList<String> phones = new ArrayList<String>();
Cursor pCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while (pCur.moveToNext())
{
phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
pCur.close();
return(phones);
}
/**
* Inserts the given relation into the stock phone book
*
* @version 1.0
* @since 2012-03-28
* @param r Relation to be added into the stock phone book
*/
public void insert(Relation r)
{
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_TYPE, null);
builder.withValue(RawContacts.ACCOUNT_NAME, null);
ops.add(builder.build());
// Name
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, r.getFirstName().replace(" ", "-")+ " " +r.getLastName().replace(" ", "-"));
ops.add(builder.build());
// Number
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, r.getNumber());
builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
ops.add(builder.build());
// Picture
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(r.getPhoto()));
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
builder.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());
}
catch (Exception e)
{
e.printStackTrace();
}
// Add the new contact
try
{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +r.getFirstName().replace(" ", "-")+ " " +r.getLastName().replace(" ", "-")+ "\" )";
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
context.startManagingCursor(c);
if (c.moveToNext())
{
r.setBook_id(new Integer(c.getString(0)).intValue());
}
else
{
r.setBook_id(-1);
}
}
/**
* Updates the given relation in the stock phone book
*
* @version 1.0
* @since 2012-03-28
* @param r Relation to be updated in the stock phone book
*/
public void update(Relation r)
{
Log.e("", ""+r.getBook_id());
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Builder builder = ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_TYPE, null);
builder.withValue(RawContacts.ACCOUNT_NAME, null);
ops.add(builder.build());
// Name
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data._ID, new String[]{String.valueOf(r.getBook_id())});
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, r.getFirstName().replace(" ", "-")+ " " +r.getLastName().replace(" ", "-"));
ops.add(builder.build());
// Number
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data._ID, new String[]{String.valueOf(r.getBook_id())});
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, r.getNumber());
builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
ops.add(builder.build());
// Picture
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(r.getPhoto()));
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data._ID, new String[]{String.valueOf(r.getBook_id())});
builder.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());
}
catch (Exception e)
{
e.printStackTrace();
}
// Update
try
{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Deletes the given relation from the stock phone book
*
* @version 1.0
* @since 2012-03-28
* @param r Relation to be removed from the stock phone book
*/
public void delete(Relation r)
{
Cursor pCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{""+r.getBook_id()}, null);
while (pCur.moveToNext())
{
String lookupKey = pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
context.getContentResolver().delete(uri, null, null);
}
}
}
回答by Manitoba
Finally, I found how to update a contact, here is the code of the update method:
终于找到了如何更新联系人,这里是更新方法的代码:
public void update()
{
int id = 1;
String firstname = "Contact's first name";
String lastname = "Last name";
String number = "000 000 000";
String photo_uri = "android.resource://com.my.package/drawable/default_photo";
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// Name
Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastname);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstname);
ops.add(builder.build());
// Number
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?"+ " AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)});
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
ops.add(builder.build());
// Picture
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(photo_uri));
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());
}
catch (Exception e)
{
e.printStackTrace();
}
// Update
try
{
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
}
The field id
is the raw contact id returned when you insert a new contact into the database. Here is the code to get this id:
该字段id
是将新联系人插入数据库时返回的原始联系人 ID。这是获取此 ID 的代码:
ContentProviderResult[] res;
try
{
res = KramerApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (res != null && res[0] != null)
{
String uri = res[0].uri.getPath().substring(14);
r.setBook_id( new Integer(uri).intValue() );
}
}
catch (Exception e)
{
e.printStackTrace();
}
Check my first question if you want to know more about how to insert/delete a contact.
如果您想了解有关如何插入/删除联系人的更多信息,请查看我的第一个问题。
回答by Max
I am not sure if this helps you, however I found a blog about the difference between contact_id and raw_contact_id:
我不确定这是否对您有帮助,但是我找到了一篇关于 contact_id 和 raw_contact_id 之间区别的博客:
http://android-contact-id-vs-raw-contact-id.blogspot.de/
http://android-contact-id-vs-raw-contact-id.blogspot.de/
In this blog he explains well how the contacts api works and if i understand it correctly you have to use the raw_contact_id to change your entry. So forget the contact_id for update and change your update so that the raw_contact_id entry is changed. I would roughly guess it would look something like this :
在这篇博客中,他很好地解释了联系人 API 的工作原理,如果我理解正确,您必须使用 raw_contact_id 来更改您的条目。因此,忘记更新的 contact_id 并更改您的更新,以便更改 raw_contact_id 条目。我大概猜想它看起来像这样:
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(RawContacts._ID + "=?", new String[]{entertheraw_contact_id})
.withValue(StructuredName.DISPLAY_NAME, "Lost Symbol Characters")
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
I hope this helps you a bit and thank you again for your help form before.
我希望这对您有所帮助,并再次感谢您之前的帮助表。
回答by ngesh
For Updating contact you need to have both contactIdand RawContactid... so while updating add this also as a value in where clause/selection args..
对于更新联系人,您需要同时拥有contactId和RawContactid...所以在更新时也将其添加为where 子句/选择参数中的一个值..
something like this..
像这样的东西..
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
//------------------------------------------------------ Names
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, data.getTitle())
.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, data.getSuffix())
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, data.getFirstName())
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, data.getMiddleName())
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, data.getSuffix())
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, data.getFirstName()+" "+data.getMiddleName()).build());