2016-08-27 2 views
0

그래서 연락처 사진을 검색하는 방법에 대해 Stackoverflow 및 Android 공식 문서를 검색해 왔으며 전혀 이해하지 못합니다. 나는 Java에 익숙하지 않아서 "Uniform Resource Identifier"외에 InputStream이 무엇인지 URI가 무엇인지 이해하지 못한다.사진이 발견되지 않았습니까?

이 때문에 나는 잘못 처리 할 방법이 없다고 생각했기 때문에 안드로이드 문서에서 코드를 복사하고 붙여 넣었습니다. 글쎄, 내가 사진을 열려고 할 때마다, 그것은 null을 반환합니다. Google은 연락처에 대해 확실히 열심히 만들었습니다. 마찬가지로 연락처의 이름을 간단하게 다시 찍는 것만 큼, 사진은 말할 것도 없습니다. 여기

일부 코드 :
openPhoto() 함수 사진 위의 코드에서

... 
InputStream stream = openPhoto(c.getID()); 
if(stream != null) 
    Log.i("PHOTO", stream.toString()); 
else 
    Log.i("NULL", "PHOTO IS NULL"); 
... 

열립니다

private InputStream openPhoto(long contactId) { 
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); 
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 

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

    if(cursor == null) { 
     return null; 
    } 

    try { 
     if(cursor.moveToFirst()) { 
      byte[] data = cursor.getBlob(0); 

      if(data != null) 
       return new ByteArrayInputStream(data); 
     } 
    } finally { 
     cursor.close(); 
    } 

    return null; 
} 

지역, 로거는 항상 로그인 "NULL을 유지 ":"PHOTO가 NULL "입니다. 그래서, 연락처 사진이 왜 여기에 없습니까?

편집 : 답변이 있다면, 무슨 일이 일어나는지 설명하십시오. 나는 어떤 대답을해도 고맙지 만, 나는 무엇이 일어나고 있는지 배우고 싶다. 지금까지는 아직 해결되지 않았습니다. 그래서 대답이 있다면, 이유를 설명하십시오. 고맙습니다.

답변

0

는 // 사용을

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

선택한 연락처의 비트 맵 이미지를 검색하려면이 기능을 구현 사진 URI

public Uri getPhotoUri(String idContact) { 
    try { 
     Cursor cur = this.ctx.getContentResolver().query(
       ContactsContract.Data.CONTENT_URI, 
       null, 
       ContactsContract.Data.CONTACT_ID + "=" + idContact+ " 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); 
} 

를 반환 //.

private void retrieveContactPhoto() { 
    Bitmap photo = null; 
    InputStream inputStream = null; 
    try { 
     inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), 
       ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID))); 
     if (inputStream != null) { 
      photo = BitmapFactory.decodeStream(inputStream); 
      ImageView imageView = (ImageView) findViewById(R.id.img_contact); 
      imageView.setImageBitmap(photo); 
     } 
    } finally { 
     if (inputStream != null) 
      try { 
       inputStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
    } 
} 
관련 문제