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)