9

I am writing an application which writes contacts in the SIM card of an Android phone. I am stuck at the point where the phone number is added: an exception occurs with no apparent reason.

Here is a snippet of code.

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContactsEntity;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.RawContacts.Entity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
[...]
try{
            // add a row to the RawContacts table
     ContentValues values = new ContentValues();
     values.put(RawContacts.ACCOUNT_TYPE, "com.anddroid.contacts.sim");
     values.put(RawContacts.ACCOUNT_NAME, "SIM");
     Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);

            // get the ID of the newly-added line
     long rawContactId = ContentUris.parseId(rawContactUri);

            // add a "name" line to the Data table, linking it to the new RawContact
            // with the CONTACT_ID column
     values.clear();
     values.put(Data.RAW_CONTACT_ID, rawContactId);
     values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
     values.put(StructuredName.DISPLAY_NAME, "Name");
     cr.insert(Data.CONTENT_URI, values);
            // this insert succeeds

            // add a "phone" line to the Data table, linking it to the new RawContact
            // with the CONTACT_ID column
     values.clear();
     values.put(Data.CONTACT_ID, rawContactId);
     values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
     values.put(Phone.NUMBER, "+12345678901");
     values.put(Phone.TYPE, Phone.TYPE_MOBILE);
     cr.insert(Data.CONTENT_URI, values);
            // this insert fails with a NullPointerException
}
catch(Exception e){
    String xx=e.toString();
    System.out.println(xx);
}

The application has permissions android.permission.READ_CONTACTS and android.permission.WRITE_CONTACTS.

The phone shows a contact with the name but no phone (incidentally, adding the phone to that contact using the normal UI results in a new contact being added, with name and phone, and the old contact with name only staying).

Any idea why the third insert (the second in the Data table) fails, while the 2 previous ones (1 in RawContacts and 1 in Data) succeed?

user377486
  • 693
  • 2
  • 10
  • 19
  • It'll really help if you post the exception stack trace too – ognian Jun 27 '10 at 18:43
  • 1
    You mean, putting a breakpoint in the catch statement, and pasting the stack trace when the breakpoint is hit (that is, the exception has been caught)? Can you see which function threw the exception from that? However, replacing Data.CONTACT_ID with Data.RAW_CONTACT_ID solved. – user377486 Jun 27 '10 at 21:14

3 Answers3

4
values.put(RawContacts.ACCOUNT_TYPE, "com.anddroid.contacts.sim");

anddroid? I didn't look at the rest, but might be worth deleting a 'd'

Philip Pearl
  • 1,523
  • 16
  • 26
2

I have replicated this one on my system,but the contact get erased as soon as the mobile restart.This means the contacts are saving in the temporary sim of mobile. Is this same happening on your side or i am missing something. I am using 3G sim.

Regards

Rohit
  • 21
  • 1
  • 1
    Hi Rohit. Actually the same happens here. Apparently, the table RawContacts is synced with the SIM phonebook at startup, but accessing it is not the same as accessing the actual SIM phonebook. I'd like to know how to do it, or at least how to trigger a resync between SIM phonebook and RawContacts... – user377486 Jul 24 '10 at 16:45
  • did you find a solution of this? – UnknownJoe Jun 23 '12 at 14:12
1

Data.CONTACT_ID has to be replaced by Data.RAW_CONTACT_ID

user377486
  • 693
  • 2
  • 10
  • 19