0

I am developing a backup application for Android, mainly contacts and SMS messages. Backing up isn't a problem, but writing the contacts back to the Android database is proving problematic.

This is what I have so far:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) 
    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
    .build()); 

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID, 1) 
    .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) 
    .withValue(StructuredName.GIVEN_NAME, "Joe") 
    .withValue(StructuredName.FAMILY_NAME, "Bloggs") 
    .build()); 

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,1)
    .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "086555555")
    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
    .build());

try{ 
    ctx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 

It all seems to hinge on the RAW_CONTACT_ID. I'm using the emulator, with no contacts at the start. It works for the first contact creation(RAW_CONTACT_ID = 0), but no contacts seem to be created after that initial one, where RAW_CONTACT_ID is 1 upwards. Anybody got any ideas as to how this is the case?

bren
  • 3
  • 2
  • this won't help you much but : why do you put the exact values instead of getting them all from the backup contact , meaning all of the keys&values (aka contentValues) ? This way , you won't miss any required field ... – android developer Feb 08 '13 at 00:36

1 Answers1

1

What I did was I created my contact first and then grabbed the ID of that newly created contact and added all the other contact details to that ID

Here is my frist step, create the new contact, then find the ID of that contact

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .build());
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, prefix)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, suffix)
                .build());

        resolver.applyBatch(ContactsContract.AUTHORITY, ops);

        Cursor cursor = null;
        try
        {
            cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, baseProjection, ContactsContract.Contacts.DISPLAY_NAME + " = ? ", new String[] {displayname}, __DEFAULT_SORT_ORDER);

            if (cursor.moveToFirst())
            {
                String val;
                val = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                return val;
            }
        }
...

Then I add any detail to the contact I want like this:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValue(ContactsContract.Data.CONTACT_ID, contactId)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.Data.DATA5, poBox)
                        .withValue(ContactsContract.Data.DATA4, street)
                        .withValue(ContactsContract.Data.DATA7, city)
                        .withValue(ContactsContract.Data.DATA8, region)
                        .withValue(ContactsContract.Data.DATA9, postCode)
                        .withValue(ContactsContract.Data.DATA10, country)
                        .withValue(ContactsContract.Data.DATA2, type)
                        .withValue(ContactsContract.Data.DATA3, label)
                        .build());
Michael Wildermuth
  • 5,762
  • 3
  • 29
  • 48