2012-01-04 157 views
0

我在收到联系人照片时遇到了问题,您在消息传递应用中看到的那种照片和新的Gmail通知。我已经看过了几个例子代码,但一切都没有工作对我来说,这是我目前有获取联系人照片

本应该得到的照片URI,并把它变成一个位图图像使用或至少它似乎

public static Bitmap getContactImage(long id,Context context){ 
    InputStream input = getPhoto(id,context); 
    if(input == null){ 
     return null; 
    } 
    return BitmapFactory.decodeStream(input); 
} 

public static InputStream getPhoto(long contactId,Context context){ 
    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); 
    Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY); 
    InputStream in = null; 
    try{ 
     in = context.getContentResolver().openInputStream(photoUri); 
    }catch(FileNotFoundException e){ 
     Log.d(TAG, e.toString()); 
    } 
    return in; 
} 

,这是我怎么称呼

long contactID = 0; 
       Bitmap image = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_contact_picture); 
       Cursor contact = context.getContentResolver().query(Data.CONTENT_URI,new String[] {Data.CONTACT_ID},Email.ADDRESS + "='" + from + "'",null,null); 
       if(contact.moveToFirst() && contact != null){ 
        contactID = contact.getLong(0); 
        image = getContactImage(contactID,context); 
       } 

我得到的接触式ID罚款(通过搜索查询对人的数量选中),但随后没有找到联系人照片。我知道有一张照片,因为我正在对自己进行测试,以确保我有一张联系照片,所以我不知道我该怎么做。

我总是发现导航联系供应商非常麻烦,因为它有这么多。

回答

1

我得到了它,我没有反对RAW_CONTACT_ID的查询与MIMETYPE,这给了我,我一直在寻找

Cursor p = context.getContentResolver().query(Data.CONTENT_URI,new String[] {Photo.PHOTO}, 
       Data.RAW_CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE+"'" 
       ,null,null); 
0

这适用于我。

//Querying for all contacts(Apply selection parameter in query to get a specific contact) 

Uri contacts = ContactsContract.Contacts.CONTENT_URI; 

cur = null; 
cur = Main.context.getContentResolver().query(contacts, null, null, 
        null, null);  

int contactIdIndex = cur.getColumnIndex(ContactsContract.PhoneLookup._ID); 
int contactId = cur.getInt(contactIdIndex); 

//照片

Uri contactUri = ContentUris.withAppendedId(
        ContactsContract.Contacts.CONTENT_URI, contactId); 

      Uri photoUri = Uri.withAppendedPath(contactUri, 
        ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 

      Cursor cursor = cr 
        .query(
          photoUri, 
          new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO }, 
          null, null, null); 

      if (cursor != null && cursor.moveToFirst()) { 
       byte[] data = cursor.getBlob(0); 

       _conEntry.setPhoto(data); 
//Data is the photo bytes for you 

      } 

      if (cursor != null) 
       cursor.close(); 
0

你就错了。

首先,从ContactsContract.Contacts表的PHOTO_ID列中获取照片的ID。接下来,通过在上一步中获得的ID,从ContactsContract.Data中的PHOTO列(实际上是DATA15的别名)中检索一个字节数组。最后,使用BitmapFactory解码该字节数组以获取位图。 Here是关于此的文档。

+0

我想,这张照片,但它没有工作,我不得不这样做'光标P = context.getContentResolver()。query(Data.CONTENT_URI,new String [] {Photo.PHOTO}, \t \t \t Data.RAW_CONTACT_ID +“=”+ contactId +“AND”+ Data.MIMETYPE +“='”+ Photo .CONTENT_ITEM_TYPE +“'” \t \t \t,null,null);'并且让我有图像 – tyczj 2012-01-05 06:02:25