用我的应用程序默认联系人照片选择器 [英] Using the default contact picker photo in my app

查看:160
本文介绍了用我的应用程序默认联系人照片选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有基本信息(姓名,电话号码),以及接触式图像,非常类似于默认的Andr​​oid联系人选取联系人的ListView。如果联系人有没有图像,它会显示在其位的应用程序图标。不过,我想使用联系人选择器使用,而不是默认的剪影图像。我的问题是我怎么能访问该图像,并在我的应用程序使用它吗?这里是我的code的加载接触图像的部分:

 联系C =新的联系人();    最终的String [] =投影新的String [] {
        Contacts.DISPLAY_NAME //联系人的名字
        Contacts.PHOTO_ID //在数据表中的列的图像的ID
    };
    最后光标接触= getContentResolver()查询(
        Contacts.CONTENT_URI,
        投影,
        接触ID的基础上Contacts._ID +=?,//过滤条目
        新的String [] {将String.valueOf(的ContactID)} //其中接触ID列相比,参数
        空值
    );    如果(contact.moveToFirst()){
        最终的字符串名称= contact.getString(
                contact.getColumnIndex(Contacts.DISPLAY_NAME));
        最后弦乐PHOTOID = contact.getString(
                contact.getColumnIndex(Contacts.PHOTO_ID));        //获取照片数据为这种接触
        如果(PHOTOID!= NULL){
            最后光标照片= managedQuery(
                Data.CONTENT_URI,
                新的String [] {} Photo.PHOTO,//为照片BLOB列
                Data._ID +=?,//选择行按id
                新的String [] {} PHOTOID,//使用PHOTOID过滤器
                空值
            );            //转换照片的blob位图
            如果(photo.moveToFirst()){
                字节[] = photoBlob photo.getBlob(
                        photo.getColumnIndex(Photo.PHOTO));
                最后的位图photoBitmap =
                    BitmapFactory.de codeByteArray的(photoBlob,0,photoBlob.length);                c.setPhoto(photoBitmap);
            }
        }        c.setName(名);

下面是我ContactAdapter类code这与接触照片ImageView的关联:

  ImageView的照片=(ImageView的)v.findViewById(R.id.contact_photo);如果(照片!= NULL){
            如果(c.hasPhoto())photo.setImageBitmap(c.getPhoto());
            其他photo.setImageResource(R.drawable.app_icon);
        }


解决方案

有3实际上是在Android的使用不同的图像。你可以找到它们来与仿真器资源的绘制文件夹中,也可以从这里下载:



In my app I have a ListView of contacts with basic information (name, phone number) as well as the contact image, very similar to the default Android contact picker. If the contact has no image, it displays the app icon in its place. However, I would like to use the default silhouette image that the contact picker uses instead. My question is how can I access this image and use it in my app? Here is the section of my code that loads the contact image:

Contact c = new Contact();

    final String[] projection = new String[] {
        Contacts.DISPLAY_NAME, // the name of the contact
        Contacts.PHOTO_ID // the ID of the column in the data table for the image
    };


    final Cursor contact = getContentResolver().query(
        Contacts.CONTENT_URI,
        projection,
        Contacts._ID + "=?", // filter entries on the basis of the contact id
        new String[] {String.valueOf(contactId)}, // the parameter which the contact id column is compared to
        null
    );

    if(contact.moveToFirst()) {
        final String name = contact.getString(
                contact.getColumnIndex(Contacts.DISPLAY_NAME));
        final String photoId = contact.getString(
                contact.getColumnIndex(Contacts.PHOTO_ID));

        // Get photo data for this contact
        if(photoId != null) {
            final Cursor photo = managedQuery(
                Data.CONTENT_URI,
                new String[] {Photo.PHOTO}, // column for the photo blob
                Data._ID + "=?", // select row by id
                new String[] {photoId}, // filter by photoId
                null
            );

            // Convert photo blob to a bitmap
            if(photo.moveToFirst()) {
                byte[] photoBlob = photo.getBlob(
                        photo.getColumnIndex(Photo.PHOTO));
                final Bitmap photoBitmap = 
                    BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);

                c.setPhoto(photoBitmap);
            }


        }



        c.setName(name);

Here is the code in my ContactAdapter class which associates the ImageView with the contacts photo:

ImageView photo = (ImageView) v.findViewById(R.id.contact_photo);

if(photo != null) {
            if(c.hasPhoto()) photo.setImageBitmap(c.getPhoto());
            else photo.setImageResource(R.drawable.app_icon);
        }

解决方案

There are actually 3 different images that are used in Android. You can find them in the drawable folder of the resources that come with the emulator, or you can download them from here:

这篇关于用我的应用程序默认联系人照片选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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