我从通讯录中提取联系人资料图片,但照片总是被拉长。

问题描述 投票:0回答:1

我获取了照片,但它不好看,它被拉伸了。我通过下面的代码来获取联系人资料图片。请看下面的图片。

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);
    }

This is image of one contact

android image contacts profile
1个回答
0
投票

我已经解决了我的问题。

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;
     }
 }

After apply below code.

© www.soinside.com 2019 - 2024. All rights reserved.