这是一个将布尔值作为我的自定义mime类型保存到联系人的示例.它使用最新的SDK 2.1
重要
此示例将DATA1用于数据,DATA1已编制索引,但不建议二进制数据.
在您的情况下,要存储二进制数据,如图片,您必须使用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();总结
以上是内存溢出为你收集整理的android – 如何以编程方式将图片(位图)分配给联系人?全部内容,希望文章能够帮你解决android – 如何以编程方式将图片(位图)分配给联系人?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)