2010-03-19 16 views
5

Ich Zuordnung zu einem Image Kontakte Bilder mit diesem Code:Wie überprüft man, ob Kontakt Bild hat?

mPhotoView = (ImageView) findViewById(R.id.photo); 
mPhotoView.setImageURI(objItem.getPhotoUri()); 

Wenn der Kontakt kein Bild hat, das nichts tut, und kein Fehler ausgelöst wird.

Wenn kein Bild vorhanden ist, möchte ich ein Standardbild hinzufügen. Also muss ich entweder überprüfen, ob das Bild zur Ansicht hinzugefügt wurde, oder überprüfen, ob der URI einige Bilddaten enthält.

Wie erreiche ich das?

als ich Standard-Bild von dieser eingestellt wird:

mPhotoView.setImageResource(R.drawable.ic_contact_picture_2); 
+0

zielen sie auf eclair oder cupcake/donut? – Schildmeijer

+0

android sdk 2.1 – Pentium10

Antwort

8

Wenn Ihr Zielgerät Android läuft 2.0/2.0.1/2.1 Sie werden wie ContactsContract.Data .CONTENT_URI mit einer Auswahl zur Abfrage haben:

Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE 

Ansonsten Abfrage Contacts.Photos .CONTENT_URI

Bearbeiten von Pentium10

Als Referenz Ich schließe hier die Methode, die ich mit oben kommen (wenn Sie noch Fehler sehen, aktualisieren):

public Uri getPhotoUri() { 
    Uri person = ContentUris.withAppendedId(
      ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getId())); 
    Uri photo = Uri.withAppendedPath(person, 
      ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 

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

Dies ist keine detaillierte Erklärung, aber wenn Sie mit ContentProvider/ContentResolver vertraut sind, sollte dies ausreichen. Ansonsten lass es mich wissen und ich werde einige Codebeispiele veröffentlichen. – Schildmeijer

+0

Danke. Ich habe deine Antwort bearbeitet und die Lösung, die mir einfällt, aufgenommen. Auf Fehler prüfen. – Pentium10

+1

aus irgendeinem Grund, manchmal, wenn Kontakt mehr Details hat, uri zurückgeben, aber URI haben kein Bild – Hamidreza

0

auf Ihrem Anwendungsfall abhängig, kann es eine andere (bessere?) Seine Option. Wenn Sie einen Mauszeiger über alle Kontakte haben und diese an eine Listenansicht binden, können Sie einfach versuchen, ein Foto mit openContactPhotoInputStream abzurufen und auf Null zu prüfen.

InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), lookupUri); 
if (stream == null) { 
    //your logic here when no photo found 
} 

Hier ist ein kurzes Beispiel für jeden, der auf diese stolpern:

import android.provider.ContactsContract.Contacts; 

//Define someDefaultPhoto somewhere accessible 

protected Bitmap getContactPhoto(Context context, Uri lookupUri) { 
    Bitmap resultPhoto; 
    InputStream stream; 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
     //For some reason this seems to be necessary pre-ICS. If anyone knows why, I'd love to hear 
     lookupUri = Contacts.lookupContact(context.getContentResolver(), lookupUri); 
    } 
    stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), lookupUri); 

    if (stream == null) { 
     resultPhoto = someDefaultPhoto; 
    } 
    else { 
     resultPhoto = BitmapFactory.decodeStream(stream); 
    } 
    return resultPhoto; 
} 

Dann können Sie es mit so etwas wie dies nennen:

int columnLookupKey = cursor.getColumnIndex(Contacts.LOOKUP_KEY); 
String lookupKey = cursor.getString(columnLookupKey); 
Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey); 
Bitmap contactPhoto = getContactPhoto(context, lookupUri); 
0

Das ist, was ich kam mit basierend auf den anderen Antworten.

private Uri getUserPictureUri(long id) { 
     Uri person = ContentUris.withAppendedId(
     ContactsContract.Contacts.CONTENT_URI, id); 
     Uri picUri = Uri.withAppendedPath(person, 
      ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
     try { 
      InputStream is = getContentResolver().openInputStream(picUri); 
      is.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      return null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return picUri; 
} 
0

Überprüfen Sie einfach, ob die photo_id Spalte in PhoneLookUp Inhalteanbieter null ist oder nicht ist hier ein Verfahren ich entwickelt habe:

public static boolean hasContactPhoto(Context context, String number) { 
    String thumbUri = ""; 
    String photoId = ""; 
    String id = ""; 
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(number)); 
    Cursor cursor = context.getContentResolver().query(uri, 
      new String[] {ContactsContract.PhoneLookup._ID,ContactsContract.PhoneLookup.PHOTO_ID}, null, null, null); 
    if (cursor.moveToFirst()) { 
     id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID)); 
     photoId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_ID)); 
    } 
    cursor.close(); 
    if(!id.equals("") && photoId != null && !photoId.equals("")) 
     return true; 
     //sms.setContactThumb(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id)).toString()); 
    else 
     return false; 
} 

wenn Sie den Kontakt Bild benötigen uri es Suche von:

ContentUris.withAppendedId (ContactsContract.Contacts.CONTENT_URI, Long.valueOf (id)). toString()

und wenn Sie es auf eine Bildansicht einstellen müssen, verwenden Sie die folgende Zeile (Ich habe Universal Image Loader 1.9 verwendet.2 für die Bildladezwecke):

 ImageLoader.getInstance().displayImage(currentRecord.getContactThumb(),viewHolder.mThumbIV); 

Anmerkung: contactThumb ist die letzte uri Prost beschrieben!

Verwandte Themen