1
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ImportContactsActivity extends Activity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button pickContact = (Button) findViewById(R.id.contacts);
        pickContact.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, 1);
            }
        });
    }

    public void onActivityResult(int reqCode, int resultCode, Intent data) {

        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
        case (1) :
          if (resultCode == Activity.RESULT_OK) {
              Uri contactData = data.getData();
              Cursor cursor =  managedQuery(contactData, null, null, null, null);
              ContentResolver cr = getContentResolver();
              Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
              if (cur.getCount() > 0) {
                  while (cur.moveToNext()) {
                      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                      String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                      if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                          Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                             new String[]{id}, null);
                          while (pCur.moveToNext()) {
                              // Do something with phones
                              TextView contactView = (TextView) findViewById(R.id.contactView);
                              contactView.setText(name.toString());
                      } 
                           pCur.close();
                      }
                  }
              }
              cursor.moveToFirst();
              String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.));

              TextView contactView = (TextView) findViewById(R.id.contactView);
              contactView.setText(number.toString());
          }
          break;
        }
    }
}

I am going to import the phone contacts into the application, and once user click on the button, the phone contact list will show up, and once the user click on one of the contacts, the contact is selected and the number will be shown on the textview. But when i click on the contact, error occurred

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-3F4927292B/1 flg=0x1 }} to activity {com.example.importcontacts/com.example.importcontacts.ImportContactsActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=609, uid=10053 requires android.permission.READ_CONTACTS

Kindly advice me on the coding, thanks

Jose Kj
  • 2,912
  • 2
  • 28
  • 40
Android_Rookie
  • 509
  • 2
  • 10
  • 25

1 Answers1

1

Seems like you forgot to add READ_CONTACTS permission in the manifest file

<uses-permission
    android:name="android.permission.READ_CONTACTS" />
bughi
  • 1,838
  • 1
  • 13
  • 24
  • ContactsContract.Contacts.XX, what should I replace for the XX in order to retrieve the phone number? – Android_Rookie May 22 '12 at 02:35
  • You might find the following links usefull http://stackoverflow.com/questions/2356084/read-all-contacts-phone-numbers-in-android http://developer.android.com/resources/articles/contacts.html – bughi May 22 '12 at 02:46
  • I have tried using this String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); But I still get this error: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r3-3F49555759/3 flg=0x1 }} to activity {com.example.antitheft/com.example.antitheft.PhoneNumActivity}: java.lang.IllegalArgumentException: column 'data1' does not exist – Android_Rookie May 22 '12 at 03:25