Question

I'm using the ContactPicker for my app. I'm using this this tutorial to build my onActivityResult method. I can't get the content revolver to build;

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

Doesn't work, the app crashes and I can't tell what kind ox exception it's throwing, if any. What am I doing wrong?

Code:

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) {  
    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  
            }   
            pCur.close();
        }  
            }  
            }  
    }
Was it helpful?

Solution

<uses-permission android:name="android.permission.READ_CONTACTS" />
Doesn't work for a permission even though it's technically correct.
<uses-permission android:name="android.permission.READ_CONTACTS"> </uses-permission>
This works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top