【问题标题】:how to get contact photo URI如何获取联系人照片 URI
【发布时间】:2011-10-12 09:50:05
【问题描述】:

我正在使用 Android Contact ContentProvider。我有一个电话号码,我需要获取与此电话号码关联的联系人的照片URI。怎么办???

我知道我可以获取照片的原始数据并构建一个InputStream,但我不想要输入流,我需要URI em>。

编辑:最初我使用以下代码来获取联系信息

    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
    Cursor cursor = context.getContentResolver().query(uri, details, null, null, null);

【问题讨论】:

    标签: android android-contentprovider


    【解决方案1】:

    要使用电话号码获取联系人 ID,请使用以下代码:

    import android.provider.ContactsContract.PhoneLookup;
    
    public String fetchContactIdFromPhoneNumber(String phoneNumber) {
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
        Cursor cursor = this.getContentResolver().query(uri,
            new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
            null, null, null);
    
        String contactId = "";
    
        if (cursor.moveToFirst()) {
            do {
            contactId = cursor.getString(cursor
                .getColumnIndex(PhoneLookup._ID));
            } while (cursor.moveToNext());
        }
    
        return contactId;
      }
    

    并使用获得的联系人 ID 来获取联系人照片 URI。使用以下代码获取照片 URI:

    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    
    public Uri getPhotoUri(long contactId) {
        ContentResolver contentResolver = getContentResolver();
    
        try {
            Cursor cursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID
                        + "="
                        + contactId
                        + " AND "
    
                        + ContactsContract.Data.MIMETYPE
                        + "='"
                        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                        + "'", null, null);
    
            if (cursor != null) {
            if (!cursor.moveToFirst()) {
                return null; // no photo
            }
            } else {
            return null; // error in cursor process
            }
    
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    
        Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, contactId);
        return Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
      }
    

    希望这会有所帮助。

    【讨论】:

    • 完成后不要忘记关闭光标,否则会导致内存泄漏。
    • 另外,请务必在您的应用清单中包含 READ_CONTACTS 权限。
    • 这个getPhotoUri proc在所有可能的方面都是完全错误的......不要使用它......你也需要检查uri的存在......应该删除答案......如果你需要uri 从内容提供商处读取 PHOTO_URI 或 PHOTO_THUMBNAIL_URI 以获得有效的 URI!
    • @xnagyg 无需任何示例,就很容易分辨什么不该做什么和该做什么。请提供您的解决方案。
    【解决方案2】:

    此解决方案演示如何从用户联系人获取图像,然后将其显示在 ImageView 中。

    ImageView profile  = (ImageView)findViewById(R.id.imageView1);                 
    Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(Contact_Id));
    InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),my_contact_Uri);            
    BufferedInputStream buf =new BufferedInputStream(photo_stream);
    Bitmap my_btmp = BitmapFactory.decodeStream(buf);
    profile.setImageBitmap(my_btmp);
    

    【讨论】:

    • 代码在BufferedInputStream中缺少“buf” buf =new BufferedInputStream(photo_stream);
    • 另外,您可以直接将Uri设置为imageView(使用imageView.setImageURI(uri),而不必自己转换为Bitmap。
    • @paul_sns 将其存储到数据库中如何?我可以使用 uri 或位图链接吗?
    【解决方案3】:

    这是来自Android Documentation的代码。

    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    return Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    

    【讨论】:

      【解决方案4】:

      您可以通过NUMBER 获取PHOTO_URI,只需使用以下代码,也可以使用_ID。

       public static String getContactPhoto(Context context, String phoneNumber) {
          ContentResolver cr = context.getContentResolver();
          Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
          Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.PHOTO_URI}, null, null, null);
          if (cursor == null) {
              return null;
          }
          String contactImage= null;
          if (cursor.moveToFirst()) {
              contactImage= cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
          }
      
          if (!cursor.isClosed()) {
              cursor.close();
          }
          return contactImage;
      }
      

      【讨论】:

        【解决方案5】:
            final boolean IS_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
            String phoneNumber = "+1 416 385 7805";
            ContentResolver contentResolver = context.getContentResolver();
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
            String[] projection = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.LOOKUP_KEY,
                    IS_HONEYCOMB ? ContactsContract.Contacts.PHOTO_THUMBNAIL_URI : ContactsContract.Contacts._ID,
                    };
            Cursor cursor =
                    contentResolver.query(
                            uri,
                            projection,
                            null,
                            null,
                            null);
            if (cursor != null && cursor.moveToNext()) {
                long contactId = cursor.getLong(0);
                String lookupKey = cursor.getString(1);
                String thumbnailUri = cursor.getString(2);           
                cursor.close();
            }
        

        所以现在如果 sdk 是蜂窝或更高版本,那么你就有了联系人的缩略图 uri。 或者你可以像这样构造一个查找 uri:

        Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
        

        附:如果您已经知道 contact id 和/或 查找键,则可以从字符串构造 Uri:

        查找:content://com.android.contacts/contacts/lookup/{lookup key}/{contact id} 缩略图:content://com.android.contacts/contacts/{contact id}/photo

        所以最好缓存这些值。

        【讨论】:

          猜你喜欢
          • 2014-01-14
          • 1970-01-01
          • 2011-11-12
          • 1970-01-01
          • 2011-08-24
          • 1970-01-01
          • 1970-01-01
          • 2011-12-31
          相关资源
          最近更新 更多