0

I'm trying to add a contact in android from my application. For that I get all the data and pass it via intent.

And, What I'm trying to do is when inserting, it opens the Add Contact screen, so the user may edit it, so I'm trying using the ACTION_EDIT.

public void addNewContactToDevice() 
    {   
        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(Data.RAW_CONTACT_ID, rawContactInsertIndex)
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.GIVEN_NAME, person.getName())
            .withValue(StructuredName.FAMILY_NAME, person.getLastName())
            .build());  
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.NUMBER, person.getPhone())
            .withValue(Phone.TYPE, Phone.TYPE_MOBILE)                
            .build());

        try 
        {
            ((Panel)activity).getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            Cursor contactsCursor = activity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                    new String[] {ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID}, 
                    ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?" , 
                    new String[] { PhoneNumberUtils.formatNumber(individualSelected.getPhone()) }, null);

            contactsCursor.moveToFirst();

            String androidContactId = contactsCursor.getString(contactsCursor.getColumnIndex(LOOKUP_KEY));

            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + androidContactId));
            ((Panel)activity).startActivityForResult(intent, 2);
        } 
        catch (RemoteException e) 
        {
            e.printStackTrace();
        } 
        catch (OperationApplicationException e) 
        {
            e.printStackTrace();
        }   

    }

But the problem is that, passing ACTION_EDIT returns a NumberFormatException for the LOOKUP_KEY (that is something like this 0r17-3E483A46442C32324838). When it tries to open the Add Contact screen, it cant parse the LOOKUP_KEY.

What am I supposed to do?

Thanks.


UPDATE: here is the logcat

10-18 08:54:59.003: ERROR/AndroidRuntime(7527): Caused by: java.lang.NumberFormatException: 0r17-3E483A46442C32324838
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.lang.Long.parse(Long.java:364)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.lang.Long.parseLong(Long.java:354)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.lang.Long.parseLong(Long.java:320)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at android.content.ContentUris.parseId(ContentUris.java:41)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at com.android.contacts.ui.EditContactActivity$QueryEntitiesTask.doInBackground(EditContactActivity.java:497)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at com.android.contacts.ui.EditContactActivity$QueryEntitiesTask.doInBackground(EditContactActivity.java:471)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at com.android.contacts.util.WeakAsyncTask.doInBackground(WeakAsyncTask.java:45)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
rogcg
  • 10,451
  • 20
  • 91
  • 133
  • Sorry. I missed up the code. The correct action I'm using is ACTION_EDIT. – rogcg Oct 17 '11 at 18:10
  • try this simple Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT); i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); i.putExtra(Insert.NAME, "TESTTEST"); i.putExtra(Insert.PHONE, "209384"); startActivity(i); If this work then check your contact URI.paarse() method in intent.setData(). – user370305 Oct 17 '11 at 18:23
  • I did it like that before. I change to the other way because I want to insert the LastName in the second field of the screen, and the last option doesnt offer that. – rogcg Oct 17 '11 at 18:32
  • I updated with the error log. – rogcg Oct 18 '11 at 10:57

1 Answers1

2

I found a solution for this, instead of passing this to the setData method:

intent.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + androidContactId));

I should be passing this:

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup_key);
Uri res = ContactsContract.Contacts.lookupContact(activity.getContentResolver(), lookupUri);

and in the setData() method, I pass the variable res, that is a URI type.


This question helped me a lot!

Community
  • 1
  • 1
rogcg
  • 10,451
  • 20
  • 91
  • 133