2013-11-21 197 views
0

我打電話意圖選擇使用此代碼的聯繫人。Android閱讀聯繫信息

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
     ((Activity) mContext).startActivityForResult(intent, PICK_CONTACT); 


    @Override 
    public void onActivityResult(int reqCode, int resultCode, Intent data) { 
     super.onActivityResult(reqCode, resultCode, data); 
     if (reqCode == PICK_CONTACT) { 
     if (resultCode == Activity.RESULT_OK) { 
       Uri uri= data.getData(); 
       } 

這裏我想獲取姓名,電話號碼和聯繫人圖片等聯繫人的三項信息。

我能夠獲取名稱和編號,但無法獲取圖像。任何幫助,將不勝感激。

+0

看看我的答案,這將幫助你。 – GrIsHu

回答

0

使用以下功能:

private String retrieveContactPhoto(String contactID) { 

    // Bitmap photo = null; 
    /* 
    * if (inputStream != null) { photo = 
    * BitmapFactory.decodeStream(inputStream); ImageView imageView = 
    * (ImageView) findViewById(R.id.img_contact); 
    * imageView.setImageBitmap(photo); } 
    */ 

    Uri photoUri = ContentUris.withAppendedId(
      ContactsContract.Contacts.CONTENT_URI, 
      Long.parseLong(contactID)); 
    final Cursor image = getContentResolver().query(photoUri, 
      PHOTO_ID_PROJECTION, null, null, 
      ContactsContract.Contacts._ID + " ASC"); 

    try { 
     Integer thumbnailId = null; 
     if (image.moveToFirst()) { 
      thumbnailId = image.getInt(image 
        .getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); 

      Uri uri = ContentUris.withAppendedId(
        ContactsContract.Data.CONTENT_URI, thumbnailId); 


      image.close(); 
      if (uri.toString().equals("content://com.android.contacts/data/0")) 
       return null; 

      return uri.toString(); 
       } 

      } finally { 

       image.close(); 
      } 
    return null; 

並顯示圖像使用下面的代碼

if (entries.get(position).getphoto() != null 
       && entries.get(position).getphoto() != "content:////com.android.contacts//data//0") { 

      try { 

       holder.photo.setImageURI(Uri.parse(entries.get(position) 
         .getphoto())); 
      } catch (Exception e) { 
       holder.photo.setImageResource(R.drawable.abc); //use default image 
      } 
     } else 
      holder.photo.setImageResource(R.drawable.abc); //use default image 
0

使用此:

InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(this.getContentResolver(), uri); 
BufferedInputStream buffer =new BufferedInputStream(input); 
Bitmap bitmap = BitmapFactory.decodeStream(buffer); 
0

嘗試以下

InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),uri);    
BufferedInputStream buf =new BufferedInputStream(photo_stream); 
Bitmap my_btmp = BitmapFactory.decodeStream(buf); 
0

檢查下面的代碼,這將有助於您獲取聯繫人姓名,號碼和聯繫人圖像。點擊一個按鈕調用下面的代碼。

public void onClickSelectContact(View btnSelectContact) { 

     // using native contacts selection 
     // Intent.ACTION_PICK = Pick an item from the data, returning what was selected. 
     startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACTS); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) { 
      Log.d(TAG, "Response: " + data.toString()); 
      uriContact = data.getData(); 

      retrieveContactName(); 
      retrieveContactNumber(); 
      retrieveContactPhoto(); 

     } 
    } 

//爲了得到接觸式圖像

private void retrieveContactPhoto() { 

     Bitmap photo = null; 

     try { 
      InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), 
        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID))); 

      if (inputStream != null) { 
       photo = BitmapFactory.decodeStream(inputStream); 
       ImageView imageView = (ImageView) findViewById(R.id.img_contact); 
       imageView.setImageBitmap(photo); 
      } 

      assert inputStream != null; 
      inputStream.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

//要獲得聯繫電話。

private void retrieveContactNumber() { 

     String contactNumber = null; 

     // getting contacts ID 
     Cursor cursorID = getContentResolver().query(uriContact, 
       new String[]{ContactsContract.Contacts._ID}, 
       null, null, null); 

     if (cursorID.moveToFirst()) { 

      contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID)); 
     } 

     cursorID.close(); 

     Log.d(TAG, "Contact ID: " + contactID); 

     // Using the contact ID now we will get contact phone number 
     Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 

       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " + 
         ContactsContract.CommonDataKinds.Phone.TYPE + " = " + 
         ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, 

       new String[]{contactID}, 
       null); 

     if (cursorPhone.moveToFirst()) { 
      contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     } 

     cursorPhone.close(); 

     Log.d(TAG, "Contact Phone Number: " + contactNumber); 
    } 

//獲取聯繫人姓名。

private void retrieveContactName() { 

     String contactName = null; 

     // querying contact data store 
     Cursor cursor = getContentResolver().query(uriContact, null, null, null, null); 

     if (cursor.moveToFirst()) { 

      // DISPLAY_NAME = The display name for the contact. 
      // HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number. 

      contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     } 

     cursor.close(); 

     Log.d(TAG, "Contact Name: " + contactName); 

    } 
} 

而且把所有的電話號碼+電子郵件地址嘗試如下:

ContentResolver contactResolver = context.getContentResolver(); 

Cursor cursor = contactResolver.query(Uri.parse(aContact.getLookupUri()), null, null, null, null); 

if (cursor != null && cursor.moveToFirst()) { 

    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)); 
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 

    if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
    { 
     Cursor pCur = contactResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId }, null); 

     while (pCur.moveToNext()) 
     { 
      String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
      String s = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), Integer.parseInt(type), ""); 


      Log.d("TAG", s + " phone: " + phone); 
     } 

     pCur.close(); 

    } 

    Cursor emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { contactId }, null); 

     while (emailCursor.moveToNext()) 
     { 
      String phone = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
      int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
      String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, ""); 

      Log.d("TAG", s + " email: " + phone); 
     } 

    emailCursor.close(); 

    cursor.close(); 
}