android - Intent.ACTION_PICK 为某些联系人返回空光标

标签 android sqlite android-intent contacts android-cursor

我有一个应用程序,其中一个方面是让用户选择一个联系人并通过该应用程序向该联系人发送文本。该应用程序仅适用于某些联系人,而对其他人则失败。更准确地说:

对于我手动输入通讯录的联系人,Intent.ACTION_PICK 可以毫不费力地找到并将它们返回到应用程序,即 cursor.moveToFirst() 是真的。

但对于由 Facebook 导入的联系人(我的手机设置为与 Facebook 联系人同步),我在点击联系人后得到以下 android.database.CursorIndexOutOfBoundsException。我有一个明显的问题是:为什么我从字面上选择了一个联系人后结果大小为 0?为什么 cursor.moveToFirst() 为假?

...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

这是我的代码:

调度 Intent :

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 结果:

(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 ...
        }

注意:我看到了联系人。但是在我选择 Facebook 导入的联系人后,我遇到了崩溃。

顺便说一句:我的代码 fragment 完全是从 android 教程中复制的:http://developer.android.com/training/basics/intents/result.html

最佳答案

您无法通过 Contacts API 访问 FB 联系人。

关于android - Intent.ACTION_PICK 为某些联系人返回空光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16577515/

相关文章:

android - 重复类 androidx.appcompat :appcompat

android - AppCompatEditText 安卓 :digits not working?

java - Android 使用 jtwitter 抛出错误

objective-c - 更新应用程序以使用以前的双 SQLite 持久存储中的核心数据

java - 将 SQL 查询结果转换为字符串数组

android - 卸载应用程序的隐含 Intent ?

android - ACTION_LOCATION_SOURCE_SETTINGS : cannot be resolved or is not a field

android - Gradle Flavor 变量的抽象为

android - Sqlite 中不存在列的返回类型?

android - 通过伪造 NFC 标签扫描来测试应用程序