How to add a new NOTE with an existing CONTACT in ANDROID - android

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);
}

Related

How to choose the ID for a new contact

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

New Contact not listed

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);

Newly added contacts not visible in Android Emulator contacts

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.

Adding two or more addresses using contactsContract in android

How can i add two or more addresses using contacts contract? I have three postal addresses to add but i don't see any secondary_address like one for phone/Email. Can anybody assist me with this?
Thanks!!
This worked for me.
where listSize -> number of contacts
for (int i = 0; i < listSize; i++) {
values.clear();
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.IS_PRIMARY, true);
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "Work "+(i+1));
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Phone.NUMBER, allContacts.get(wanted.get(i)).getPhone());
getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
values.put(StructuredPostal.TYPE, Phone.TYPE_CUSTOM);
values.put(StructuredPostal.LABEL, "Work "+(i+1));
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(StructuredPostal.STREET, allContacts.get(wanted.get(i)).street);
values.put(StructuredPostal.CITY, allContacts.get(wanted.get(i)).city);
values.put(StructuredPostal.REGION, allContacts.get(wanted.get(i)).state);
values.put(StructuredPostal.POSTCODE, allContacts.get(wanted.get(i)).zipCode);
getContentResolver().insert(Data.CONTENT_URI, values);
}

Inserting contacts in Android 2.2

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.

Categories

Resources