2016-01-19 108 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"));