Code example for ContentResolver

Methods: insertopenAssetFileDescriptorqueryupdate

0
	public void updatePhoto(ContentResolver cr, byte[] photo, String id, boolean markDirty, boolean fromthumb) { 
		if (cr == null || id == null) {
			return; 
		} 
		 
		long rawId = queryForRawContactId(cr, Long.parseLong(id));
		if (rawId < 0) {
			return; 
		} 
		//if(!fromthumb) 
			writeDisplayPhoto(cr, rawId, photo);
		 
		if(photo == null)
		{ 
			id = String.valueOf(rawId);
			ContentValues values = new ContentValues(); 
			int photoRow = -1; 
			 
			// query for existing photo 
			String where = ContactsContract.Data.RAW_CONTACT_ID + " == " + 
				id + " AND " + Data.MIMETYPE + "=='" + 
				ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; 
			Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, where, null, null); 
			int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
			int idphotoid = cursor.getColumnIndexOrThrow(ContactsContract.Data.PHOTO_FILE_ID);
			long photo_file_id = 0;
			if(cursor.moveToFirst()){ 
				photoRow = cursor.getInt(idIdx); 
				photo_file_id = cursor.getLong(idphotoid);
			}  
			cursor.close();
			if (photoRow < 0 && photo == null) {
				return; 
			} 
			 
			//long photo_file_id = MainActivity.GetInstance().GetPhotoStore().insert(displayPhoto, photo, true); 
			values.put(ContactsContract.Data.RAW_CONTACT_ID, 
					id); 
			values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1); 
			values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo);
			values.put(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID, photo_file_id);
			values.put(ContactsContract.Data.MIMETYPE, 
					ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
			// append CALLER_IS_SYNCADAPTER to prevent sync 
			Uri uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoRow);
			//Uri.Builder builder = ContactsContract.Data.CONTENT_URI.buildUpon(); 
			//builder.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true"); 
			Uri updateUri = uri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
			if(photoRow >= 0){ 
				if (photo == null) {
					//cr.delete(builder.build(), ContactsContract.Data._ID  
					//	+ " = " + photoRow, null); 
					//Log.d(TAG, "deleting"); 
					//cr.delete(uri, null, null); 
					cr.update(uri, values, null, null);
				} else { 
					//cr.update  
					//	(builder.build(), values, ContactsContract.Data._ID  
					//	+ " = " + photoRow, null); 
					cr.update(uri, values, null, null);
				} 
			} else {  
				//cr.insert(builder.build(), values); 
				uri = ContactsContract.Data.CONTENT_URI;
				//updateUri = uri.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); 
				cr.insert(uri, values);
			} 
		} 
	} 
	 
	public Uri writeDisplayPhoto(ContentResolver cr, long rawContactId, byte[] photo) {
	     Uri rawContactPhotoUri = Uri.withAppendedPath(
	             ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
	             RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
	     try { 
	         AssetFileDescriptor fd = cr.openAssetFileDescriptor(rawContactPhotoUri, "rw");
	         OutputStream os = fd.createOutputStream();
	         if(photo == null)
	        	 photo = new byte[1];
	         os.write(photo);
	         os.close();