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
|
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