I am trying to insert new RawContact contacts, but the RawContact added doesn't get displayed when I view the contacts through Contacts or phonebook. As I understand if we create a RawContact and there is no contact associated with it then the contact will be automatically inserted. I get a valid value of rawContactId and no exceptions are thrown, so I assume the insertion is successful. Am I doing anything wrong or am I missing something? I am using the code example from developer site, just pasting it here:
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);
I thought this Q was long forgotten but Since someone upvoted it, I am assuming someone else also faces the same problem as me. After a little struggle I was able to figure out the problem and insert contacts, Hope this helps, here is the sample code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME,null )
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "9X-XXXXXXXXX")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
.build());
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
A client reported to me that the solution in the answer above(by Als) doesn't work on some HTC devices. So after a few days of frustration I came up with this solution:
String name = "First Family";
String phone = "0123456789";
ContentValues values = new ContentValues();
values.put(Data.DISPLAY_NAME, name);
Uri rawContactUri = c.getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
long contactId = Util.getContactId(c, rawContactId);
System.out.println("rawContactId = " + rawContactId);
System.out.println("contactId = " + contactId);
values.clear();
values.put(Phone.NUMBER, phone);
values.put(Phone.TYPE, Phone.TYPE_OTHER);
values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
values.put(Data.MIMETYPE, Data.CONTENT_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);
public static long getContactId(Context context, long rawContactId) {
Cursor cur = null;
try {
cur = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts.CONTACT_ID }, ContactsContract.RawContacts._ID + "=" + rawContactId, null, null);
if (cur.moveToFirst()) {
return cur.getLong(cur.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cur != null) {
cur.close();
}
}
return -1l;
}
To have a visible contact created, it needs to belong to a visible group. Have a look at gmail contacts on your computer to view groups and visibility.
To find a visible group on the device, do something like this:
Long myContactsGroupId = null;
sqlWhere = ContactsContract.Groups.ACCOUNT_TYPE + " = 'com.google' AND " + ContactsContract.Groups.GROUP_VISIBLE + " = 1";
Cursor cursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[] {"_id"}, sqlWhere, null, "_id");
if (cursor.moveToFirst()) {
myContactsGroupId = cursor.getLong(0);
}
To add the group to the rawContact:
cv.clear();
cv.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
cv.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, myContactsGroupId);
cv.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, cv);
Or the ops version:
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, myContactsGroupId)
.build());
#anqe1ki11er:
I don't understand the 3rd paragraph in your answer where it says:
values.put(Data.MIMETYPE, Data.CONTENT_TYPE)
...
There is no such MIMETYPE. (I checked it on HTC Desire running HTC Android 2.2).
Thanks.
Related
I want to create a contact in the device's address book.
The answer to the question here and the documentation indicates that we must provide the ID for the contact.
Here is the example from the docs:
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
How can I choose/generate this ID ? if I choose a random value, it might conflict with an existing contact ?
if you looked at the Documentation
you will find a table describes all fields, among them is RAW_CONTACT_ID, i quote
The id of the row in the ContactsContract.RawContacts table that this data belongs to.
so this is the ID of an inserted record at RawContacts table, as if it's master detail schema
check this link for more info about RawContacts
so what i think is you have to insert a rawContact first, get that (automatically generated id)
and use it for inserting a contact
something like this
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long insertedRawContactId = ContentUris.parseId(rawContactUri);
then use insertedRawContactId to insert contact.
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, insertedRawContactId );
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
check this site for more info to make it clear.
and regarding the 3 types or 3 elements of contact, check this image
and check this answer points 1,2,3,... etc
hope this help you more understanding
try like this,
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactInsertIndex);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
hope it will help you
i successfully updated the existing NOTE in a contact using
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
this method doesn't work for adding a new NOTE..
How to add a new NOTE with an existing CONTACT in ANDROID?
public void testInsert()
{
ContentValues values = new ContentValues();
// insert a empty note into RawContacts.CONTENT_URI, and get the rawContactId
Uri rawContactUri = this.getContext().getContentResolver().insert(RawContacts.CONTENT_URI, values);
// get id
long rawContactId = ContentUris.parseId(rawContactUri);
// insert name data
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId); // id
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// MIMETYPE
values.put(StructuredName.GIVEN_NAME, "zetsin");// first name
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
// insert phone
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "123456");
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
// insert email
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.DATA, "zetsin#gmail.com");
values.put(Email.TYPE, Email.TYPE_WORK);
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
}
I used this code to add the contact:
public static long addNewNameToContact(Context context, String name) {
long rawContactId = 0;
ContentValues values = new ContentValues();
values.put(Contacts.DISPLAY_NAME, name);
Uri rawContactUri = context.getContentResolver().insert(RawContacts.CONTENT_URI, values);
rawContactId = ContentUris.parseId(rawContactUri);
return rawContactId;
}
(I need to create a contact only with the name and then add phone and other data).
Can you help me?
Very thanks,
Mateus
First, you need to create a raw contact specifying the account type and name. The account type and name can be any string. For instance accountType="com.mateus.app" and accountName="user":
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
Then, you can set the display name with the raw contact ID that is returned above:
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);
I am adding new contacts as follows:-
ContentValues values = new ContentValues();
String accountType = "test#gmail.com";
values.put(RawContacts.ACCOUNT_TYPE, accountType );
String accountName = "com.google";
values.put(RawContacts.ACCOUNT_NAME, accountName );
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
//Add contact number
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "123456789");
getContentResolver().insert(Data.CONTENT_URI, values);
//Type of phone number
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.TYPE, Phone.TYPE_WORK);
getContentResolver().insert(Data.CONTENT_URI, values);
//Add contact name
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(Data.DISPLAY_NAME, "First Second");
getContentResolver().insert(Data.CONTENT_URI, values);
Query runs successfully. But the added contacts are not visible in Android Emulator Contacts. Please help. Am I missing anything here.
I have name, phone number and E-mail infomation of a contact. I just want to insert the additional email and phone for the existing contact. My questions are
How to find the contact is already existing or not?
How to insert the values on the additional or secondary address option?
Thanks in Advance.
In the official document has new contancts api.
http://developer.android.com/reference/android/provider/ContactsContract.Data.html
First, look up raw contacts id with your criteria, such as name:
final String name = "reader";
// find "reader"'s contact
String select = String.format("%s=? AND %s='%s'",
Data.DISPLAY_NAME, Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
String[] project = new String[] { Data.RAW_CONTACT_ID };
Cursor c = getContentResolver().query(
Data.CONTENT_URI, project, select, new String[] { name }, null);
long rawContactId = -1;
if(c.moveToFirst()){
rawContactId = c.getLong(c.getColumnIndex(Data.RAW_CONTACT_ID));
}
c.close();
Second, use rawContactId to add an entry to contacts:
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
PS. don't forget the permissions:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>