Android ContactContacts StreamItem

Contact
|
RawContact
/ \
/ \
Data StreamItem
|
StreamItemPhoto

Android通讯录中数据库表关系如上所示,一条rawContact关联多条streamItem数据,但是数量是有限制的,可以使用如下方法查询:
int maxLength = 0;
Cursor c = getContentResolver().query(StreamItems.CONTENT_LIMIT_URI,
new String[] { StreamItems.MAX_ITEMS }, null, null, null);

try {
c.moveToFirst();
maxLength = c.getInt(0);
} finally {
c.close();
}


那么如何添加StreamItem呢

private static void addContactStreamItem(Context context, long rawContactId, String text, String comments, long timestamp, String accountName, String accountType){ ContentValues values = new ContentValues(); values.put(StreamItems.RAW_CONTACT_ID, rawContactId); values.put(StreamItems.TEXT, "Breakfasted at Tiffanys"); values.put(StreamItems.TIMESTAMP, timestamp); values.put(StreamItems.COMMENTS, comments); values.put(StreamItems.ACCOUNT_NAME, accountName); values.put(StreamItems.ACCOUNT_TYPE, accountType); context.getContentResolver().insert(StreamItems.CO NTENT_URI, values); }You can also specify an action that should be executed when a stream item or one of its photos is tapped. To achieve this, specify the receiving Activities in your contacts.xml using the tags viewStreamItemActivity and viewStreamItemPhotoActivity:

pictures were written using a ContentValues object, just like all the other data rows of the raw contact. While this approach is still supported, it might fail when used with bigger pictures, as there is a size limit when sending ContentValues across process boundaries. The prefered way now is to use an AssetFileDescriptor and write them using a FileOutputStream instead:


private static void saveBitmapToRawContact(Context context, long rawContactId, byte[] photo) throws IOException { Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI , rawContactId); Uri outputFileUri = Uri.withAppendedPath(rawContactUri, RawContacts.DisplayPhoto.CONTENT_DIRECTORY); AssetFileDescriptor descriptor = context.getContentResolver().openAssetFileDescript or( outputFileUri, "rw"); FileOutputStream stream = descriptor.createOutputStream(); try { stream.write(photo); } finally { stream.close(); descriptor.close(); } }For best results, store uncompressed square photos and let the contacts provider take care of compressing the photo. It will create both a thumbnail and a display photo as necessary.
This API is available on API version 14+. For older versions, we recommend to fallback to the old method using ContentValues and assuming a constant size of 96x96.

http://android-developers.blogspot.com/2012/02/new-social-apis-in-android-ics.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值