2016-01-19 60 views
0

我试图阅读完整的个人资料信息,如(全名,电话,地址,邮件....)。如何访问ContactsContract配置文件?

我到处搜索了一个很好的示例代码。我尝试了很多方法(Uri => Cursor)来访问配置文件。 在这个时候,我只能取得全名(个人资料联系人),没有其他任何东西。

我可以使用意图获取其他联系人的数据,发送到联系人应用程序,但我不能读取配置文件联系人(只是全名)。

我在清单文件中添加了READ_PROFILE权限。

用下面的代码我得到的全名(我也​​可以访问单独姓和名):

Uri uriProfile = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, 
        ContactsContract.Contacts.Data.CONTENT_DIRECTORY); 
Cursor cursorProfile = this.getContentResolver().query(uriProfile, 
        null, null, null, null); 
String projection = Profile.DISPLAY_NAME; 
String profileName = cursorProfile.getString(cursorProfile.getColumnIndex(projection); 

但是当我使用这下面的投影拿到电话号码,它会返回一个错误,应用程序停止工作:

String projection = ContactsContract.CommonDataKinds.Phone.NUMBER 
+0

哪些错误你好吗? –

+0

'DatabaseUtils#dumpCursor'在'logcat'上显示的是什么? – pskink

+0

@MarcoFerrari现在我没有任何错误,但使用什么投影并不重要,我总是有名称。你有我的一些解决方案read_profile – Abderrezak

回答

1

我找到了解决方法使用此代码:

  1. 得到一个URI简介
  2. 创建一个指向URI内容的光标,其空投影。因为对于配置文件,数据的保存方式与普通联系人不同。
  3. 使用MIMETYPE将光标指向想要的数据。

Uri uriProfile = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY); Cursor cursorProfile = getApplicationContext().getContentResolver().query(uriProfile, null, null, null, null); String cursorProfile_MIMIETYPE = cursorProfile.getString(cursorProfile.getColumnIndex("MIMETYPE"));