14

I have an app in which one aspect is for a user to select a contact and send a text to that contact thru the app. The app only works with some contacts and fails on others. More precisely:

for the contacts that I entered into my contact book by hand, the Intent.ACTION_PICK has no trouble finding and returning them to the app, i.e. cursor.moveToFirst() is true.

But for the contact that were imported by Facebook (my phone is set to sync with Facebook contacts), I get the following android.database.CursorIndexOutOfBoundsException after I click on the contact. One glaring question I have is: why is the result size 0 after I literally selected a contact? Why is cursor.moveToFirst() false?

...Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at com.company.Game.SendTextActivity.onActivityResult(SendTextActivity.java:118)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.app.Activity.dispatchActivityResult(Activity.java:5436)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3188)
05-15 17:57:04.741: E/AndroidRuntime(21301):    ... 11 more

Here is my code:

dispatchIntent:

Intent pickContactIntent = new Intent(Intent.ACTION_PICK,  Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);

onActivity result:

(requestCode == PICK_CONTACT_REQUEST) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = { Phone.NUMBER };

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            //do work with number here ...
        }

Note: I see the contacts. But after I select the Facebook imported contact, I get the crash.

BTW: My code snippet is copied from the android tutorial exactly: http://developer.android.com/training/basics/intents/result.html

Pouton Gerald
  • 1,585
  • 21
  • 30
  • Note that for the Facebook contacts `cursor.moveToFirst()` returns false. I am not merely interested in a bandaid such as catching the exception. I know how to check `cursor.moveToFirst()`. What I need to know is how to get the numbers for those contacts. – Pouton Gerald May 18 '13 at 21:11
  • This comment is unrelated to your question... Since you are using a projection, you do not need to call .getColumnIndex(...). The column index will match the index of that column in the projection string array. – abh May 25 '13 at 04:13

2 Answers2

2

You cannot access FB contacts through the Contacts API.

stoilkov
  • 1,666
  • 1
  • 11
  • 18
2

To fix the crash you should check the result of moveToFirst() like this:

String number = null;
if (cursor.moveToFirst()) {
   number = cursor.getString(0); // 0 matches the index of NUMBER in your projection.
}

To explore the nature of the data that is available to you I would pass in "null" for the projection so that all of the fields come back, and dump the field names and values. You may find the data you are looking for, just not in the NUMBER field.

abh
  • 433
  • 2
  • 9