如何以编程方式指定图片(位图),以联系人?位图、联系人、方式、图片

2023-09-06 15:44:18 作者:死于脚气攻心

我有位图型的变量,我想将它分配给从我的联系人列表中的CalledID图片联系人,我将如何做到这一点?

I have a variable of type Bitmap and I would like to assign it to a Contact from my contact list as the CalledID picture, how would I do that?

推荐答案

您必须创造自己的MIME类型的。

You have to creat your own mime type for those.

下面是保存一个布尔值,我的自定义MIME类型来接触一个例子。它采用最新的SDK 2.1

Here is an example that saves a boolean as my custom mime type to the contacts. It uses the latest SDK 2.1

重要

本例使用DATA1的数据,DATA1被索引,但它不推荐用于二进制数据。 你的情况来存储二进制数据,比如图片你必须使用DATA15

This example uses DATA1 for data, DATA1 is indexed but it's not recomended for binary data. In your case to store binary data such as Picture you have to use DATA15.

按照惯例,DATA15用于存储BLOB(二进制数据)。

public static final String MIMETYPE_FORMALITY = "vnd.android.cursor.item/useformality";
public clsMyClass saveFormality() {
        try {
            ContentValues values = new ContentValues();
            values.put(Data.DATA1, this.getFormality() ? "1" : "0");
            int mod = ctx.getContentResolver().update(
                    Data.CONTENT_URI,
                    values,
                    Data.CONTACT_ID + "=" + this.getId() + " AND "
                            + Data.MIMETYPE + "= '"
                            + clsContacts.FORMALITY_MIMETYPE + "'", null);

            if (mod == 0) {
                values.put(Data.CONTACT_ID, this.getId());
                values.put(Data.MIMETYPE, clsContacts.FORMALITY_MIMETYPE);
                ctx.getContentResolver().insert(Data.CONTENT_URI, values);
            }
        } catch (Exception e) {
            Log.v(TAG(), "saveFormality failed");
        }
     return this;
    }

public boolean getFormality() {
     if (data.containsKey(FORMALITY)) {
        return data.getAsBoolean(FORMALITY);
    } else {
        // read formality
        Cursor c = readDataWithMimeType(clsContacts.MIMETYPE_FORMALITY, this.getId());
        if (c != null) {
            try {
                if (c.moveToFirst()) {
                    this.setFormality(c.getInt(0) == 1);
                    return (c.getInt(0) == 1);
                }
            } finally {
                c.close();
            }
        }
        return false;
    }

}
public clsMyClass setFormality(Boolean value) {
    data.remove(FORMALITY);
    data.put(FORMALITY, value);
    return this;
}

/**
 * Utility method to read data with mime type
 *
 * @param mimetype String representation of the mimetype used for this type
 *            of data
 * @param contactid String representation of the contact id
 * @return
 */
private Cursor readDataWithMimeType(String mimetype, String contactid) {
    return ctx.getContentResolver().query(
            Data.CONTENT_URI,
            new String[] {
                Data.DATA1
            },
            Data.RAW_CONTACT_ID + "=" + contactid + " AND " + Data.MIMETYPE + "= '" + mimetype
                    + "'", null, null);
}

用法

objContact.setFormality(true).saveFormality();
 
精彩推荐
图片推荐