6

I have an issue while I try to copy a contact which exists in the android contacts application to the SIM card. Following is the code:

ContentValues cv = new ContentValues();
cv.put("tag", cName);
cv.put("number", cNumber);

Uri uri = context.getContentResolver().insert(SIM_CONTENT_URI, cv);
Log.d(TAG_LOG, "URI is : " + uri);

I have values inside cName and cNumber variables. But when I print the log to see the value of the uri variable: it is null.

Can anyone please let me know if I have gone wrong somewhere in the code above for inserting to SIM?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
user264953
  • 1,837
  • 8
  • 37
  • 63

2 Answers2

1

I just have implemented a simple code to insert contact in SIM card, maybe it can help you:

private void insertSIMContact(String number, String name) {
     Uri simUri = Uri.parse("content://icc/adn");
     ContentValues values = new ContentValues();
     values.put("number", number);
     values.put("tag", name);
     getContentResolver().insert(simUri, values);
}
Nguyen
  • 2,019
  • 1
  • 24
  • 31
  • 1
    Tried that on an HTC One. For some reasons, the numbers added in the above manner didn't show up immediately, but only after rebooting the phone. It is as if the contacts are correctly added on the SIM, but the phone does not pick them up: it only does at startup. Is there some way to trigger an explicit sync of the contacts from the SIM? – user377486 Jul 08 '13 at 21:30
0

Try this code..

String name = "First Family";
String phone = "0123456789";
ContentValues values = new ContentValues();
values.put(Data.DISPLAY_NAME, name);
Uri rawContactUri = c.getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
long contactId = Util.getContactId(c, rawContactId);
System.out.println("rawContactId = " + rawContactId);
System.out.println("contactId = " + contactId);

values.clear();
values.put(Phone.NUMBER, phone);
values.put(Phone.TYPE, Phone.TYPE_OTHER);
values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);

values.clear();
values.put(Data.MIMETYPE, Data.CONTENT_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);

values.clear();
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
values.put(Data.RAW_CONTACT_ID, rawContactId);
c.getContentResolver().insert(Data.CONTENT_URI, values);



public static long getContactId(Context context, long rawContactId) {
Cursor cur = null;
try {
    cur = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[] { ContactsContract.RawContacts.CONTACT_ID }, ContactsContract.RawContacts._ID + "=" + rawContactId, null, null);
    if (cur.moveToFirst()) {
        return cur.getLong(cur.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (cur != null) {
        cur.close();
    }
}
return -1l;
}

for completely understanding see this

Community
  • 1
  • 1
Kiran
  • 3,095
  • 5
  • 23
  • 38