我查看了地址簿并尝试获取所有联系人'照片不是空的。
我使用的是Android API8,所以我无法查询image_uri。
给定photo_id(API 5),我如何获得照片位图?
这是我的查询:
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);
...
答案 0 :(得分:1)
试试这个:
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 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;
}
答案 1 :(得分: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);
}