2014-07-23 3 views
0

현재 Android 기기에서 연락처 전화 번호를 조회하고 연락처 이름 및 연락처 이미지와 같은 연락처 정보를 검색하려고합니다. 계약 이름을 얻는 것은 정상적으로 작동하지만 사진 URL을 얻으려고 할 때 null 포인터가 던져지고 있습니다. 당신이 제공 할 수있는 어떤 도움Android에서 연락처 이미지 검색

public ContactInformation getContactInfoFromPhoneNumber(String number) 
    { 
     ContactInformation contactInformation = new ContactInformation(); 
     if (number == null) 
     { 
      return null; 
     } 
     number = number.replace(" ", ""); 

     if (number.startsWith("+")) 
     { 
      number = number.substring(3); 
     } 

     ContentResolver contentResolver = context.getContentResolver(); 

     Uri uri = ContactsContract.Data.CONTENT_URI; 
     String[] projection = new String[] {ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME}; 
     String selection = "REPLACE (" + ContactsContract.CommonDataKinds.Phone.NUMBER + ", \" \" , \"\") LIKE ?"; 
     String[] selectionArgs = { "%" + number }; 
     Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, null); 

     if (cursor.moveToFirst()) 
     { 
      contactInformation.contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
      contactInformation.photoUri = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 
      cursor.close(); 

      return contactInformation; 
     } 
     else 
     { 
      cursor.close(); 
      return null; 
     } 
    } 

감사 : 아래

내가 사용하고있는 코드입니다.

UPDATE는 아래

은 Itzik 사마라 답변을 내 업데이트 된 코드를 기반으로합니다. 내 연락처 조회를하고있는 중이 야 방법 아래

다음
public ContactInformation getContactInfoFromPhoneNumber(String number) 
    { 
     ContactInformation contactInformation = new ContactInformation(); 
     if (number == null) 
     { 
      return null; 
     } 
     number = number.replace(" ", ""); 

     if (number.startsWith("+")) 
     { 
      number = number.substring(3); 
     } 

     ContentResolver contentResolver = context.getContentResolver(); 

     Uri uri = ContactsContract.Data.CONTENT_URI; 
     String[] projection = new String[] {ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME}; 
     String selection = "REPLACE (" + ContactsContract.CommonDataKinds.Phone.NUMBER + ", \" \" , \"\") LIKE ?"; 
     String[] selectionArgs = { "%" + number }; 
     Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, null); 

     if (cursor.moveToFirst()) 
     { 
      contactInformation.contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
      contactInformation.photoBase64String = getContactPhoto(cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID))); 
      cursor.close(); 

      return contactInformation; 
     } 
     else 
     { 
      cursor.close(); 
      return null; 
     } 
    } 

내 getContactPhoto 기능입니다

private String getContactPhoto(int contactID) 
    { 
     Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactID); 
     Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
     Cursor cursor = context.getContentResolver().query(photoUri, new String[]{ContactsContract.Contacts.Photo.PHOTO}, 
       null, null, null); 
     if (cursor == null) 
     { 
      return null; 
     } 
     if (cursor.getCount() > 0) 
     { 
      while (cursor.moveToNext()) { 
       byte[] data = cursor.getBlob(0); 
       String base64String = Base64.encodeToString(data, Base64.DEFAULT); 
       cursor.close(); 
       return base64String; 
      } 
     } 
     return null; 
    } 

자사가 null을 반환하는 경우 명세서에 실패하고 경우 바로 밖으로 뛰어 마치 커서에 아무 것도 없습니다.

답변

-1

이 기능은 나를 위해 작동 :

private byte[] getContactPhoto(Context context,long contact_id) { 

    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contact_id); 
    Uri photoUri = Uri.withAppendedPath(contactUri,ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
    Cursor cursor = context.getContentResolver().query(photoUri, new String[]{ContactsContract.Contacts.Photo.PHOTO}, null, null, null); 
    if(cursor == null) 
     return null; 
    try { 
     if(cursor.getCount() > 0) { 
      if (cursor.moveToFirst()) { 
       byte[] data = cursor.getBlob(0); 
       cursor.close(); 
       if (data != null) 
        return data; 
      } 
     } 

    } 
    finally { 
     cursor.close(); 
    } 
    return null; 
} 




    private void getContacts(Context context) { 

    ContentResolver cr = context.getContentResolver(); 
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 
    try { 
     if (cursor.getCount() > 0) { 

      while (cursor.moveToNext()) { 

       long contact_id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       Contact friend = new Contact(); 
       String name= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       String email = getContactEmail(context, contact_id); 
       if(!name.contains("@") && !email.matches("")) { 
        friend.setName(name); 
        friend.setEmail(email); 
        friend.setImage(getContactPhoto(context, contact_id)); 
        friend.setPhone(getContactMobilePhoneNumber(context, contact_id)); 

        mContacts.add(friend); 
       } 
      } 

     } 
    } 
    finally { 
     cursor.close(); 
    } 



} 
+0

이 어떤 특별한 권한이 필요합니까? –

+0

contact_id가 – Boardy

+0

권한을 참조하는 것은 무엇입니까?

관련 문제