How to check if Contact has Image?
I am assigning to an ImageView contacts images using this code:
mPhotoView = (ImageView) findViewById(R.id.photo);
mPhotoView.setImageURI(objItem.getPhotoUri());
If the contact has no image, this does nothing, and no error is raised.
When there is no image, I want to add a default image. So I need to check either if the image was added to the view, or check that the URI holds some image data
How do I achieve that?
Than I will set default image by this:
mPhotoView.setImageResource(R.drawable.i开发者_运维问答c_contact_picture_2);
If your target device is running android 2.0/2.0.1/2.1 you will have to query ContactsContract.Data.CONTENT_URI with a selection like:
Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE
Otherwise query Contacts.Photos.CONTENT_URI
Edit by Pentium10
For reference I include here the method I come up with (if you still see bugs, update it):
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;
}
Depending on your use case, there may be another(better?) option. If you have a cursor of all contacts and you're say, binding them to a listview, you can simply attempt to retrieve a photo using openContactPhotoInputStream, and check for null.
InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), lookupUri);
if (stream == null) {
//your logic here when no photo found
}
Here's a quick example for any who may stumble upon this :
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;
}
Then you can call it with something like this:
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);
This is what I came up with based on the other answers.
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;
}
Just check if the PHOTO_ID column in PhoneLookUp Content provider is null or not Here is a method I've developed:
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;
}
if you need the contact image uri get it from:
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id)).toString()
and if you need to set it to an image view use the following line (I've used Universal Image Loader 1.9.2 for image loading purposes):
ImageLoader.getInstance().displayImage(currentRecord.getContactThumb(),viewHolder.mThumbIV);
note : contactThumb is the last described uri Cheers!
精彩评论