android 擷取通訊錄連絡人資訊

來源:互聯網
上載者:User

  android 中擷取通訊錄中連絡人資訊,代碼如下:

  

    /**     * 擷取連絡人資訊,包括:名稱、號碼、頭像     * @return     */    private ArrayList<PhoneContactor> getPhoneContacts(){            Log.i(TAG, "getPhoneContacts");            ContentResolver resolver=this.getContentResolver();        ArrayList<PhoneContactor> list = new ArrayList<PhoneContactor>();             String[] columns = new String[] {Phone.DISPLAY_NAME,Phone.NUMBER,Phone.PHOTO_ID,Phone.CONTACT_ID};            Cursor cursor = resolver.query(Phone.CONTENT_URI, columns, null,null, null);   Bitmap photo_default=BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo_default);           while (cursor.moveToNext()) {                PhoneContactor pc = new PhoneContactor();                                String name=cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));                                String number=cursor.getString(cursor.getColumnIndex(Phone.NUMBER));                if(TextUtils.isEmpty(number)){ //判斷號碼是否為空白                                continue;                }                                long photoId=cursor.getLong(cursor.getColumnIndex(Phone.PHOTO_ID));                                long contactId=cursor.getLong(cursor.getColumnIndex(Phone.CONTACT_ID));                                //得到連絡人頭像Bitamp                   Bitmap contactPhoto = null;                                //photoId 大於0表示連絡人有頭像,否則設定一個預設頭像                   if(photoId > 0 ) {                                      Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactId);                      InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);                      contactPhoto = BitmapFactory.decodeStream(input);                                  }else {                                      contactPhoto = photo_default;                 }                                  pc.name(name);                pc.number(number);                pc.photo(contactPhoto);                                list.add(pc);                                 Log.d(TAG, "name:" + pc.name());                Log.d(TAG, "number:" + pc.number());        }                cursor.close();                 return list;    }

其中:PhoneContactor為自訂的儲存連絡人資訊的類,最後別忘了加上許可權:

<uses-permission android:name="android.permission.READ_CONTACTS"/> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.