Java 将联系人照片添加到ImageView的简单方法?

Java 将联系人照片添加到ImageView的简单方法?,java,android,view,Java,Android,View,我在获取和设置联系人的图像作为视图背景时遇到问题,令人惊讶的是,关于如何做到这一点的示例很少。我正在尝试构建一个类似于People的应用程序,它可以显示大的联系人照片 这就是我现在正在做的: Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id)); InputStream input = ContactsContract.Contacts.openContactPh

我在获取和设置联系人的图像作为视图背景时遇到问题,令人惊讶的是,关于如何做到这一点的示例很少。我正在尝试构建一个类似于People的应用程序,它可以显示大的联系人照片

这就是我现在正在做的:

Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
Bitmap bm = BitmapFactory.decodeStream(input);
Drawable d = new BitmapDrawable(bm);
button.setBackgroundDrawable(drawable);
但是它使用的URI会得到一个缩略图,所以即使有一张大照片,当缩放到适合imageView时,图像看起来也很糟糕。我知道另一种获取URI的方法,它实际上会获取一张大照片,这是:

final Uri imageUri = Uri.parse(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)));

但是我还没有成功地将它放到imageView中,也许上面的代码可以修改为使用第二个uri。如果你知道如何使用第二个uri,或者如果有比通过uri更容易获得联系人图像的方法,请告诉我。任何信息都将被感谢

在获取URI方面做得很好。你快到了。首先,考虑使用光照法来代替光照URI,因为它可能是你所需要的尺寸。

编辑:仅供参考,从API11开始,照片缩略图URI可用。您仍然可以有条件地使用它

如果你想使用一个外部库,“”肯定是你想要的,因为从几天前的1.7.1版本开始,它增加了对内容方案的支持,而且它非常智能,内存明智。它还有很多定制选项

编辑:此库已失效。改用。

如果您希望更好地控制最终的捆绑包大小并自己编写代码

您需要获取并解码该内容的输入流;这应该在后台线程上完成。看看这个纵容的方法;您可以使用图像视图和您获得的uri初始化它,并在希望加载图像视图时启动它

private class ContactThumbnailTask extends AsyncTask<Void, Void, Bitmap> {

    private WeakReference<ImageView> imageViewWeakReference;
    private Uri uri;
    private String path;
    private Context context;


    public ContactThumbnailTask(ImageView imageView, Uri uri, Context context) {
        this.uri = uri;
        this.imageViewWeakReference = new WeakReference<ImageView>(imageView);
        this.path = (String)imageViewWeakReference.get().getTag(); // to make sure we don't put the wrong image on callback
        this.context = context;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        InputStream is = null;
        try {
            is = context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Bitmap image = null;
        if (null!= is)
            image=  BitmapFactory.decodeStream(is);

        return image;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewWeakReference != null && imageViewWeakReference.get() != null && ((String)imageViewWeakReference.get().getTag()).equals(path) && null != bitmap)
            imageViewWeakReference.get().setImageBitmap(bitmap);
    }
}
私有类ContactThumbnailTask扩展了AsyncTask{
私有WeakReference图像视图WeakReference;
私有Uri;
私有字符串路径;
私人语境;
公共联系人ThumbnailTask(ImageView ImageView、Uri、上下文){
this.uri=uri;
this.imageViewWeakReference=新的WeakReference(imageView);
this.path=(String)imageViewWeakReference.get().getTag();//确保回调时没有放置错误的图像
this.context=上下文;
}
@凌驾
受保护位图doInBackground(无效…参数){
InputStream=null;
试一试{
is=context.getContentResolver().openInputStream(uri);
}catch(filenotfounde异常){
e、 printStackTrace();
}
位图图像=空;
if(null!=is)
image=BitmapFactory.decodeStream(is);
返回图像;
}
@凌驾
受保护的void onPostExecute(位图){
if(imageViewWeakReference!=null&&imageViewWeakReference.get()!=null&&((字符串)imageViewWeakReference.get().getTag()).equals(路径)&&null!=bitmap)
imageViewWeakReference.get().setImageBitmap(位图);
}
}

您可以尝试使用SmartImageView:扩展imageview并异步加载图像。

使用外部库来完成此操作。或者浏览代码,以自己的方式制作类似的东西

以下是我在自己的几个应用程序上使用的一个:

代码如下所示:

Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id));  
UrlImageViewHelper.setUrlDrawable(button, uri.toString(), R.drawable.dummy_contact_photo); 

这可能会对您有所帮助(联系人由getId()标识):

用法是:

Uri u = objItem.getPhotoUri();
if (u != null) {
        mPhotoView.setImageURI(u);
} else {
        mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}

这是一个建议。首先,我知道一件事

设置联系人图像时。首先,Android显示该图像的裁剪活动。喜欢

****仔细看上图。Android将图像裁剪为方形。并将方形图像作为水滴存储在contacts中。(它不是单个图像,而是水滴)。
您可以从编码中获得图像视图的方形图像。所以顶部和底部只显示黑色。因为你的手机是长方形的****

如果要显示全屏图像。请通过编程方式设置联系人的大图像。互联网上有很多例子


祝你一切顺利。如果你有任何疑问。请提供评论。

您可以使用以下方法轻松地将联系人照片设置为图像视图

public String getImageUriString(String phoneNumber)
{
    ContentResolver resolver = context.getContentResolver();
    Cursor names = resolver.query(
            Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)),
            null, null, null, null);

    names.moveToFirst();
    String name = "";
    if(!names.isAfterLast())
    {
        name = names.getString(names.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));

    }
    else
    {
        name = null;
    }

    names.close();

    return name;
}





public void setImageView(ImageView contactPhoto) {

String photoUriString = di.getImageUriString(contactNumber);
        if(photoUriString != null) {
            Uri photoUri = Uri.parse(photoUriString);
            contactPhoto.setImageURI(photoUri);
        } else {
            contactPhoto.setImageResource(R.drawable.icon_contact);
        }
}

在类中,使用从上述方法获取的uri设置图像视图。

要做到这一点,只需添加最后一个参数preferHighres=true:

openContactPhotoInputStream (ContentResolver cr, Uri contactUri, boolean preferHighres)
如果preferHighres为真,并且联系人有更高分辨率的照片可用,则返回。如果为false,则此函数始终尝试获取缩略图

     Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id));
     InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri, true);
所有的图像可能都有不同的大小。为了调整它们的大小,我使用下一个代码:

    Bitmap bm = BitmapFactory.decodeStream(input);
    bm = Bitmap.createScaledBitmap(photo, contactImageWidth, contactImageheight, false);
    Drawable d = new BitmapDrawable(getContext(), bm);
    button.setBackgroundDrawable(d);

我想这就是你想要的,我还没能实现。它需要变量url和file
ImageOperations(这个,url,filename)
我不知道它们是什么。如果你知道如何使用url和文件名,我会接受你的回答。我注意到,更改为ImageView并使用setImageUri()实际上可以将图像设置得很好。但是有一个小问题,我不知道为什么,但是我在ImageView的顶部和底部都有一个空白。如果你知道如何修复它,我会接受答案我无法实现这个顺便说一句,我所有的ImageView中都有空格使用setImageUri有问题吗?使用backgrond线程会更有效吗TimageURI在UI线程上进行解码,这会导致延迟中断,所以我建议不要使用它。您查看过ImageView的scaletype吗?尺寸固定吗?
    Bitmap bm = BitmapFactory.decodeStream(input);
    bm = Bitmap.createScaledBitmap(photo, contactImageWidth, contactImageheight, false);
    Drawable d = new BitmapDrawable(getContext(), bm);
    button.setBackgroundDrawable(d);