[英]Get high resolution contact photo
有誰知道如何檢索電話簿聯系人(用戶)的高質量照片? 我所有的嘗試都導致圖像質量低下...
沒關系,如果我是通過“ openContactPhotoInputStream”或“ Uri”檢索到的,則兩者的質量都不高。 它看起來像縮略圖。
我的方法:
private void retrieveContactPhoto(ImageView imgView, Long contactId) {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts
.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
contactId));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
imgView.setImageBitmap(photo);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Uri getPhotoUri(long contactId) {
ContentResolver contentResolver = MainActivity.instance
.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);
}
Min SDK設置為android 4.3版。 謝謝。
使用openContactPhotoInputStream
的另一種重載方法(在API級別14中添加),該方法的布爾值指示您更喜歡高分辨率照片。
public static InputStream openContactPhotoInputStream
(ContentResolver cr, Uri contactUri, boolean preferHighres)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.