Android阅读联系信息

时间:2013-11-21 11:27:08

标签: 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();
                }

在这里,我想获取三个联系信息,例如姓名,电话号码和联系人图片。

我可以获取名称和号码,但无法获取图片。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用此:

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

答案 2 :(得分:0)

尝试以下

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

答案 3 :(得分: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();
}