2012-08-14 2 views
0

Android 앱에서 작업 중입니다. 내 앱에서 연락처 표시 이름과 displaypic을 표시해야합니다. 숫자로 연락처 표시 이름을 가져 오는 경우 다음 코드를 사용하고 있습니다.연락처로 사진 찍기

public String getContactDisplayNameByNumber(String number) { 
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 
    String name = " "; 

    ContentResolver contentResolver = getContentResolver(); 
    Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID, 
      ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null); 

    try { 
     if (contactLookup != null && contactLookup.getCount() > 0) { 
      contactLookup.moveToNext(); 
      name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); 

      //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID)); 
     } 
    } finally { 
     if (contactLookup != null) { 
      contactLookup.close(); 
     } 
    } 

    return name; 
} 

잘 작동합니다. 연락해주세요. 이런 식으로 연락주세요.

+1

이것은 아마도 http://stackoverflow.com/a/8695649/704374 – Akshay

+0

@ Akshay..You 만 나는 그것을 확인하지 않은 이유 displayname..Thats에 대한 질문 right..But이다 중복. 도와 주셔서 감사합니다. 대답으로 게시하면 받아 들일 수 있습니다. :) – sarath

답변

1

다음 코드는 저에게 효과적입니다.

public Drawable getContactDisplaypicByNumber(String number) { 
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 

    String contactId = null; 
    InputStream input = null; 
    String[] projection = new String[] { 
      ContactsContract.PhoneLookup.DISPLAY_NAME, 
      ContactsContract.PhoneLookup._ID}; 
     Drawable d=null; 

    ContentResolver contentResolver = getContentResolver(); 
    Cursor contactLookup = contentResolver.query(uri,projection, null, null, null); 


    if (contactLookup.moveToFirst()) { 
     contactId = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.PhoneLookup._ID)); 

     Uri uri1 = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId)); 
     input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri1); 


    } 
    else { 

     Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found @ " + number); 

     return d; // contact not found 

    } 

    if (input == null) { 
     Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name); 
     d=getResources().getDrawable(R.drawable.ic_launcher); 
     return d; // no photo 
    } else { 

     Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name); 
     d=Drawable.createFromStream(input, ""); 
     System.out.println("ddddddddddddddddddddddddddddddddd"+d); 
     return d; 
    } 

}