2017-06-19 2 views
1

Ich verwende den folgenden Code, um auf die Telefonnummern und die Namen des Kontakts zuzugreifen. Ich möchte dieser Funktion einen Weg hinzufügen, um auch das Foto des Kontakts hinzuzufügen.Ich möchte das Kontaktfoto für meine Kontakte abrufen. Wie gehe ich vor?

void loadContacts() { 
    ContentResolver contentResolver=getContentResolver(); 
    Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 
    if(cursor.getCount() > 0) { 
     while (cursor.moveToNext()) {String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
      if (hasPhoneNumber > 0) { 
       Cursor cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); 
       while (cursor2.moveToNext()) { 
        String phoneNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        make_contact(name,phoneNumber); 
       } 
       cursor2.close(); 
      } 
     } 
    } 
    cursor.close(); 
} 
+0

folgen Sie diesem Link https://gist.github.com/evandrix/7058235 –

Antwort

1

Für Bitmap,

private Bitmap retrieveContactPhoto(String contactID) { 

      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); 
       } 

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

       return photo ; 

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

     } 

Mithilfe der Kontakt-ID können Sie ein Bitmap-Bild abrufen und auf Bildansicht festlegen.

+0

verwenden Sie diese Long.valueOf (contactID) anstelle dieser neuen Long (contactID). – RameshJaga

+0

Auch dieser Code erstellt NullPointerException beim Schließen der inputStream.close(); Führen Sie vor dem Schließen des IOstream eine Nullprüfung durch. – RameshJaga

+0

Ihr Code liest das Kontaktbild und funktioniert einwandfrei. – RameshJaga

0

versuchen diese

public Uri getPhotoUri() { 
try { 
    Cursor cur = this.ctx.getContentResolver().query(
      ContactsContract.Data.CONTENT_URI, 
      null, 
      ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND " 
        + ContactsContract.Data.MIMETYPE + "='" 
        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null, 
      null); 
    if (cur != null) { 
     if (!cur.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, Long 
     .parseLong(getId())); 
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 

}

nun auf Bild

Uri u = objItem.getPhotoUri(); 
if (u != null) { 
    mPhotoView.setImageURI(u); 
} else { 
    mPhotoView.setImageResource(R.drawable.ic_contact_picture_2); 
} 
Verwandte Themen