2

我正在尝试从 android 电话簿中导入联系人。该应用程序在所有手机上都能正常运行,但在 HTC 手机上失败。我的代码如下

 Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
        pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);


Uri contactUri = data.getData();
                String[] projection = {Phone.DISPLAY_NAME,Phone.NUMBER};

                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);
                int nameColumn=cursor.getColumnIndex(Phone.DISPLAY_NAME);
                String number = cursor.getString(column);
                String fName=cursor.getString(nameColumn);
                contact = new HashMap<String, String>();
                contact.put(fName, number);
                eName.setText(fName);

因为它是在客户端电话上,所以我无法查看日志。

4

2 回答 2

1

Android 对其联系人有几个抽象(Contact <- RawContact <- Data),以便能够分离不同的提供者(Google、FB 等)并聚合它们。该文档很好地概述了联系人提供程序的工作原理:

http://developer.android.com/guide/topics/providers/contacts-provider.html

我认为您的问题是,您正在尝试访问联系人表中的数据字段。Intent.ACTION_PICK 返回联系人 uri,它指向联系人表。

您需要做的是从contacts 表中获取Contacts._ID,然后从ContactsContract.Data 表中获取按Contacts._ID 过滤的数据。您可以在此处找到示例查询:

http://developer.android.com/reference/android/provider/ContactsContract.Data.html

于 2013-01-27T10:05:35.957 回答
0

这是用户经历的崩溃吗?可能是由于某些设备在没有名称/号码的情况下在 NUMBER 的 DISPLAY_NAME 中返回 null(如果设备具有 Skype 等自定义内容提供商,则可能会发生这种情况)。一些设备返回 display_name 中的数字,一些设备返回 null。

还是只是用户看不到任何联系人?你知道用户是否真的有吗?例如,如果通讯录为空,您的代码会发生什么情况。

于 2013-01-27T10:05:55.547 回答