如何将默认图像设置为没有以前图像的Android手机联系人 [英] How to set default image to Android phone contact that has no previous image

查看:96
本文介绍了如何将默认图像设置为没有以前图像的Android手机联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码可以更新Android联系人的图像,问题是当联系人没有以前的图像时它不起作用.我还检查了联系人是否来自电话"帐户或"*@gmail.com"帐户.当这些帐户已经有一张图片时,我没有问题更新该图片,问题就出在联系人没有分配以前的图片时.

I have a piece of code which updates the an Android contact´s image, the problem is that it doesn't work when the contact has no previous image. I also checked that the contact was from "Phone" account or "*@gmail.com" account. When it already has an image with these accounts I have no problem updating the image, the problem is just when the contact has no previous image assigned.

这是负责更新图片的方法.

Here is the method in charge of updating the image.

public void update(long id, Bitmap bitmap) {       

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    // Picture
    try
    {
        ByteArrayOutputStream image = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);

        Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
        builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
        builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", 
                new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
        builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
        ops.add(builder.build());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    try
    {
        this.context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

这是我如何知道联系人是否属于我上面提到的两个帐户之一:

And here is how I know if the contact belongs to one of the two accounts I mentioned above:

// Getting the raw contact id
public static int getRawContactId(Context context, long id) {

    String[] projection = new String[] { ContactsContract.RawContacts._ID };
    String selection = ContactsContract.RawContacts.CONTACT_ID + "=?";
    String[] selectionArgs = new String[] { String.valueOf(id) };
    Cursor c = context.getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI, projection,
            selection, selectionArgs, null);
    int rawContactId = -1;
    if (c.moveToFirst()) {
        rawContactId = c.getInt(c
                .getColumnIndex(ContactsContract.RawContacts._ID));
    }

    return rawContactId;
}

在这里我获得了帐户名称以供进一步分析

Here I get the account name for further analysis

//...
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
String[] rawProjection = { RawContacts._ID, RawContacts.ACCOUNT_NAME };
Cursor raw = context.getContentResolver().query(rawContactUri, rawProjection, null, null, null);
if (raw.moveToFirst()) {
    account = raw.getString(1);
}

谢谢.

推荐答案

问题似乎是,正如您所描述的,您正在更新图片 http://developer.android.com/reference/android/content/ContentProviderOperation.html#newUpdate(android.net.Uri)

The problem seems to be that, as you describe, you're updating the image http://developer.android.com/reference/android/content/ContentProviderOperation.html#newUpdate(android.net.Uri)

代替插入一个新的 http://developer.android.com/reference/android/content/ContentProviderOperation.html#newInsert(android.net.Uri)

如果联系人没有以前的图像,则不可能更新数据库中的字段,因为该字段不存在.您应该执行插入操作.

If the contact doesn't has a previous image, it's impossible to update the field in the database because it doesn't exists. You should perform an insert operation instead.

如果api方法在运行时硬抛出异常失败,这是有争议的.

希望有帮助.

这篇关于如何将默认图像设置为没有以前图像的Android手机联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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