2014-04-23 2 views
1

나는 주소록을 살펴보고 null이 아닌 모든 연락처 사진을 얻으려고합니다.photo_id에서 photo_uri를 가져 오는 방법

android API8을 사용 중이므로 image_uri를 쿼리 할 수 ​​없습니다.

주어진 photo_id (API 5), 어떻게 사진 비트 맵을 얻을 수 있습니까?

private void GetImageByPhoneNumber(String number) 
    { 
     Uri uri1 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
       Uri.encode(number)); 
     Cursor test = getContentResolver().query(uri1, 
       new String[] { "photo_uri" }, null, null, 
       null); 

     if (test.getCount() > 0) { 
      test.moveToFirst(); 

      Uri photoUri = Uri.parse(test.getString(test 
        .getColumnIndexOrThrow("photo_uri")))); 

      Bitmap image = getPhoto(context , photoUri); 


     } 

     test.close(); 
    } 

및 getPhoto :

public final static String[] PROJECTION2 = new String[] { 
     ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, 
     ContactsContract.CommonDataKinds.Phone.NUMBER, 
     ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
     ContactsContract.CommonDataKinds.Phone.PHOTO_ID 
}; 

public static void fillAddressBookData() { 
    String sHash = null; 
    String where = ContactsContract.Contacts.IN_VISIBLE_GROUP + "= ? "; 
    String[] selectionArgs = new String[] {"1"}; 
    Cursor cursor = 
     AppService 
      .getAppContext() 
      .getContentResolver() 
      .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION2, where, 
       selectionArgs, null); 

... 

답변

1

이 시도 : 여기

내 쿼리입니다

public static Bitmap getPhoto(Context context , Uri uri){ 

     Bitmap bm = null; 
     try { 
      bm = BitmapFactory.decodeStream(context.getContentResolver() 
        .openInputStream(uri)); 


     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     if (bm == null) { 
      bm = BitmapFactory.decodeResource(context.getResources(), 
        R.drawable.default_user_avatar); 
     } 
     return bm; 
    } 
+0

photo_uri id API 11 –

+0

죄송합니다. 몰랐습니다. http://stackoverflow.com/questions/2383580/how-do-i-load-a-contact-photo를 보았습니까? –

0

다음 코드를 시도, 그것은 트릭을 할해야

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

커서를 전혀 사용하지 않습니까? –

+0

당신이 내 대답은 아직 괜찮아요 사진 uri 따라서 질문을 기다릴 - 사진에 대한 커서 쿼리 사진이없는 경우에는 사진이 없습니다 uri ... – crazyPixel