【问题标题】:Android - Get Contact Photo from phone numberAndroid - 从电话号码获取联系人照片
【发布时间】:2015-04-01 02:14:14
【问题描述】:

如何从联系人的地址(电话号码)获取联系人照片?

【问题讨论】:

标签: android photo contact


【解决方案1】:
public static Bitmap retrieveContactPhoto(Context context, String number) {
        ContentResolver contentResolver = context.getContentResolver();
        String contactId = null;
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

        String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};

        Cursor cursor =
                contentResolver.query(
                        uri,
                        projection,
                        null,
                        null,
                        null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
            }
            cursor.close();
        }

        Bitmap photo = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.default_image);

        try {
            if(contactId != null) {
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
                    ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactId)));

                if (inputStream != null) {
                    photo = BitmapFactory.decodeStream(inputStream);
                }

                assert inputStream != null;
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return photo;
    }

【讨论】:

  • 感谢此解决方案运行良好,但您必须在打开输入流之前检查 contactId 是否为空
【解决方案2】:

我认为这个版本在所有 android 版本中效果更好:

public   Bitmap getContactsDetails(String address) {
    Bitmap bp = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.contact_default_picture);





    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));

    // querying contact data store
    Cursor phones = context.getContentResolver().query(contactUri, null, null, null, null);


    while (phones.moveToNext()) {
        String image_uri = phones.getString(phones.getColumnIndex(
                ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

        if (image_uri != null) {

            try {
                bp = MediaStore.Images.Media
                        .getBitmap(context.getContentResolver(),
                                Uri.parse(image_uri));

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return   bp;

}

【讨论】:

    【解决方案3】:

    调用此方法获取所有联系信息。

    public void readContacts() {
      StringBuffer sb = new StringBuffer();
      sb.append("......Contact Details.....");
      ContentResolver cr = getContentResolver();
      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        null, null, null);
      String phone = null;
      String emailContact = null;
      String emailType = null;
      String image_uri = "";
      Bitmap bitmap = null;
      if (cur.getCount() > 0) {
       while (cur.moveToNext()) {
        String id = cur.getString(cur
          .getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur
          .getString(cur
            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    
        image_uri = cur
          .getString(cur
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
        if (Integer
          .parseInt(cur.getString(cur
            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
         System.out.println("name : " + name + ", ID : " + id);
         sb.append("\n Contact Name:" + name);
         Cursor pCur = cr.query(
           ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
           null,
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID
             + " = ?", new String[] { id }, null);
         while (pCur.moveToNext()) {
          phone = pCur
            .getString(pCur
              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          sb.append("\n Phone number:" + phone);
          System.out.println("phone" + phone);
         }
         pCur.close();
    
         Cursor emailCur = cr.query(
           ContactsContract.CommonDataKinds.Email.CONTENT_URI,
           null,
           ContactsContract.CommonDataKinds.Email.CONTACT_ID
             + " = ?", new String[] { id }, null);
         while (emailCur.moveToNext()) {
          emailContact = emailCur
            .getString(emailCur
              .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
          emailType = emailCur
            .getString(emailCur
              .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
          sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
          System.out.println("Email " + emailContact
            + " Email Type : " + emailType);
    
         }
    
         emailCur.close();
        }
    
        if (image_uri != null) {
         System.out.println(Uri.parse(image_uri));
         try {
          bitmap = MediaStore.Images.Media
            .getBitmap(this.getContentResolver(),
              Uri.parse(image_uri));
          sb.append("\n Image in Bitmap:" + bitmap);
          System.out.println(bitmap);
    
         } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
    
        }
    
    
        sb.append("\n........................................");
       }
    
       textDetail.setText(sb);
      }
     }
    

    【讨论】:

    • tnx 我使用你的代码和选择参数,所以我可以按数字检索照片
    【解决方案4】:

    您可以使用下面的列获取联系人照片 uri,ContactsContract.CommonDataKinds.Phone.PHOTO_URI

    【讨论】:

    • 好的,所以我已经尝试过了,它肯定不起作用。为什么 4 up-votes 我不明白这个网站。我可以从 ContactsContracts.CommonDataKinds.Phone.CONTENT_URI 获取信息。但似乎不可能从 CommonDataKinds 获得与照片相关的任何内容
    【解决方案5】:

    这是函数:

        public static  Bitmap getContactsDetails(String address) {
            Bitmap bp = BitmapFactory.decodeResource(context.getResources(),
                             R.drawable.default_contact_photo);
            String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = '" + address + "'"; 
            Cursor phones = context.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection,
                    null, null);
            while (phones.moveToNext()) {
                String image_uri = phones.getString(phones.getColumnIndex(
    ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
    
                 if (image_uri != null) {
                     try {
                      bp = MediaStore.Images.Media
                        .getBitmap(context.getContentResolver(),
                          Uri.parse(image_uri));
    
                     } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     }
                 }  
            }
            return bp;
        }
    

    【讨论】:

      【解决方案6】:

      如果您想获得检索 更大的照片

      ,您可以进行一些更改
       public  Bitmap retrieveContactPhoto(Context context, String number) {
              ContentResolver contentResolver = context.getContentResolver();
              String contactId = null;
              Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
      
              String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};
      
              Cursor cursor =
                      contentResolver.query(
                              uri,
                              projection,
                              null,
                              null,
                              null);
      
              if (cursor != null) {
                  while (cursor.moveToNext()) {
                      contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
                  }
                  cursor.close();
              }
      
              Bitmap photo = BitmapFactory.decodeResource(context.getResources(),
                      R.mipmap.popup);
      
              try {
      
                  Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(contactId));
                  Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
      
                      AssetFileDescriptor fd =
                              getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
                      InputStream inputStream=fd.createInputStream();
      
      
                  if (inputStream != null) {
                      photo = BitmapFactory.decodeStream(inputStream);
                  }
      
                  assert inputStream != null;
                  inputStream.close();
      
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return photo;
          }
      

      用于检索缩略图大小的照片

       public InputStream openPhoto(long contactId) {
           Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
           Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
           Cursor cursor = getContentResolver().query(photoUri,
                new String[] {Contacts.Photo.PHOTO}, null, null, null);
           if (cursor == null) {
               return null;
           }
           try {
               if (cursor.moveToFirst()) {
                   byte[] data = cursor.getBlob(0);
                   if (data != null) {
                       return new ByteArrayInputStream(data);
                   }
               }
           } finally {
               cursor.close();
           }
           return null;
       }
      

      更多详情:

      Click Here

      【讨论】:

      • 所以缩略图是Contacts.Photo.CONTENT_DIRECTORY,完整的照片是Contacts.Photo.DISPLAY_PHOTO
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 2016-01-08
      • 2014-05-26
      • 2017-04-13
      • 2015-07-20
      • 1970-01-01
      • 2013-05-19
      相关资源
      最近更新 更多