Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent.ACTION_PICK returns empty cursor for some contacts

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

like image 601
Pouton Gerald Avatar asked May 16 '13 01:05

Pouton Gerald


1 Answers

You cannot access FB contacts through the Contacts API.

like image 188
stoilkov Avatar answered Sep 20 '22 13:09

stoilkov