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
Related
I'm working on an app that create contact using Intent. Why using Intent you will ask : Because It's to let user modify the contact as they want before registering.
My problem is while I'm using StructuredName or StructuredPostal, for other CommonDataKinds it's working well. Here's my code:
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
ContentValues row1 = new ContentValues();
row1.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
row1.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "company");
row1.put(ContactsContract.CommonDataKinds.Organization.TITLE, "position");
data.add(row1);
ContentValues row3 = new ContentValues();
row3.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
row3.put(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_HOME);
row3.put(ContactsContract.CommonDataKinds.Website.URL, "website");
data.add(row3);
Here is StructuredPostal
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
data.add(row5);
i = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
i.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivity(i);
Due to this problem I momentarily did a patch :
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
i.putExtra(ContactsContract.Intents.Insert.POSTAL, "street, code city, country");
Someone knows what's wrong?
Thanks for answers :)
For the StructuredPostal add the type. And use a different mimetype. Do not add null values for the address fields like street etc.
ContentValues row5 = new ContentValues();
row5.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country");
row5.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME);
data.add(row5);
StructuredName does not work for me either.
For using StructuredName in the Intent.INSERT you should add both Insert.Name and data row for StructuredName
i.putExtra(ContactsContract.Intents.Insert.NAME, "Firstname LASTNAME");
ContentValues name = new ContentValues();
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Firstname");
name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "LASTNAME");
data.add(name);
Same goes for StructuredPostal. You need to add Insert.POSTAL and the StructuredPostal data row.
The Insert.Name is taken as the DISPLAY_NAME and the rest of the fields are used for StructuredName. The Insert.POSTAL is used as formatted address. This is why you need to provide both data.
To see how android processes your Intent, check method parseStructuredNameExtra in this file
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>