如何检查是否有联系的图像? [英] How to check if Contact has Image?

查看:205
本文介绍了如何检查是否有联系的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code指派给ImageView的联系人图片:

  mPhotoView =(ImageView的)findViewById(R.id.photo);
mPhotoView.setImageURI(objItem.getPhotoUri());
 

如果该联系人有没有图像,这并没有什么,而不会引发错误。

在没有图像,我想补充一个默认的图像。所以,我需要检查或者在拍摄时添加到视图,或检查URI包含一些图像数据。

我如何做到这一点?

比我将设置默认的图像通过这样的:

  mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
 

解决方案

如果您的目标设备运行的是Android 2.0 / 2.0.1 / 2.1,你将不得不查询<一个href="http://developer.android.com/reference/android/provider/ContactsContract.Data.html">ContactsContract.Data.CONTENT_URI一个选择,如:

  Data.MIMETYPE +=+ Photo.CONTENT_ITEM_TYPE
 

否则查询<一href="http://developer.android.com/reference/android/provider/Contacts.Photos.html">Contacts.Photos.CONTENT_URI

修改通过Pentium10

有关参考我在这里有我想出的方法(如果你仍然看到错误,更新):

 公众开放的getPhotoUri(){
    乌里人= ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI,的Long.parseLong(的getId()));
    乌里照片= Uri.withAppendedPath(人,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

    光标CUR = this.ctx
            .getContentResolver()
            .query(
                    ContactsContract.Data.CONTENT_URI,
                    空值,
                    ContactsContract.Data.CONTACT_ID
                            +=
                            + this.getId()
                            +和
                            + ContactsContract.Data.MIMETYPE
                            +=
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                            +',NULL,NULL);
    如果(CUR!= NULL){
        如果(!cur.moveToFirst()){
            返回null; //没有照片
        }
    } 其他 {
        返回null; //在光标过程中的错误
    }
    返回的照片;
}
 

I am assigning to an ImageView contacts images using this code:

mPhotoView = (ImageView) findViewById(R.id.photo);
mPhotoView.setImageURI(objItem.getPhotoUri());

If the contact has no image, this does nothing, and no error is raised.

When there is no image, I want to add a default image. So I need to check either if the image was added to the view, or check that the URI holds some image data

How do I achieve that?

Than I will set default image by this:

mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);

解决方案

If your target device is running android 2.0/2.0.1/2.1 you will have to query ContactsContract.Data.CONTENT_URI with a selection like:

Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE

Otherwise query Contacts.Photos.CONTENT_URI

Edit by Pentium10

For reference I include here the method I come up with (if you still see bugs, update it):

public Uri getPhotoUri() {
    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getId()));
    Uri photo = Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

    Cursor cur = this.ctx
            .getContentResolver()
            .query(
                    ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID
                            + "="
                            + this.getId()
                            + " AND "
                            + ContactsContract.Data.MIMETYPE
                            + "='"
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                            + "'", null, null);
    if (cur != null) {
        if (!cur.moveToFirst()) {
            return null; // no photo
        }
    } else {
        return null; // error in cursor process
    }
    return photo;
}

这篇关于如何检查是否有联系的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆