android - 我从通讯录中获取联系人个人资料图片,但照片总是被拉伸
问题描述
我拿照片,但它看起来不太好,它被拉伸了。我通过以下代码获取联系人个人资料图像。请找到附图。
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(columnIndex)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
//contactIm.setImageBitmap(photo);
if (photo == null) {
/*String letter = gc.split_word(cName);
TextDrawable drawable = gc.name_image(letter);
contact_image.setImageDrawable(drawable);*/
} else {
contactIm.setImageBitmap(photo);
}
解决方案
我已经解决了我的问题。
imageView.setImageBitmap(BitmapFactory.decodeStream(openDisplayPhoto(contactId)));
public InputStream openDisplayPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri displayPhotoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.DISPLAY_PHOTO);
try {
AssetFileDescriptor fd =
getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
return fd.createInputStream();
} catch (IOException e) {
return null;
}
}
推荐阅读
- python-3.x - 在单独的图上绘制图例时出错
- laravel - 服务器上的 Laravel 存储图像未显示
- laravel - 未定义变量:标题
- swiftui - 在 iOS 14 上获取小部件的数据
- python-3.x - 如何在python中打印最大数量的列表项和索引?
- vue.js - 如何收听在 vue js 中以编程方式创建的子事件?
- java - 如何阻止 Spring JPA 将参数转义到查询方法?
- python - 按其他列填充 NaN 值
- javascript - indexeddb 从索引 n 到索引 k 获取数据
- php - 请问如何在php MVC框架的多个文件夹中为任何类控制器动态设置命名空间