Hi, 
I am Adding Some Contacts to Native Android Phone Book, After executing 
below code i am able to see the person in my android phone book, but when i 
open the contact card of the person i inserted, contact card crashes. 
Below is my code.

 public static void insertAndroidContact(Context context, String name, 
String Phone, String email)
    {
        ArrayList <ContentProviderOperation> ops = new 
ArrayList<ContentProviderOperation>();
        
        ContentProviderOperation.Builder op =
                
ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, 
"Custom")
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, name);
        ops.add(op.build());

     // Creates the display name for the new raw contact, as a 
StructuredName data row.
        op =
                
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.DISPLAY_NAME, 
name);

        // Builds the operation and adds it to the array of operations
        ops.add(op.build());

        op =
                
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 
Phone)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
"Mobile");

        // Builds the operation and adds it to the array of operations
        ops.add(op.build());

        // Inserts the specified email and type as a Phone data row
        op =
                
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, 
email)
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
"com.example");

        /*
         * Demonstrates a yield point. At the end of this insert, the batch 
operation's thread
         * will yield priority to other threads. Use after every set of 
operations that affect a
         * single contact, to avoid degrading performance.
         */
        op.withYieldAllowed(true);

        // Builds the operation and adds it to the array of operations
        ops.add(op.build());
        
        try {
            
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } 
        catch (Exception e) {

            // Display a warning
            //Context ctx = getApplicationContext();
            
            CharSequence txt = "Contact Creation Failure";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, txt, duration);
            toast.show();

        }
    }

Thank you

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to