[wp8] now pupulates contact photos
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/commit/23152e83 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/23152e83 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/23152e83 Branch: refs/heads/master Commit: 23152e8387f61f88a138655387f9ce22b76cd309 Parents: 71ad712 Author: sgrebnov <[email protected]> Authored: Fri Apr 25 16:28:08 2014 -0700 Committer: sgrebnov <[email protected]> Committed: Fri Apr 25 16:28:08 2014 -0700 ---------------------------------------------------------------------- src/wp/ContactsHelper.cs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/23152e83/src/wp/ContactsHelper.cs ---------------------------------------------------------------------- diff --git a/src/wp/ContactsHelper.cs b/src/wp/ContactsHelper.cs index 9f6a4cd..80166fa 100644 --- a/src/wp/ContactsHelper.cs +++ b/src/wp/ContactsHelper.cs @@ -24,6 +24,7 @@ namespace WPCordovaClassLib.Cordova.Commands using System.Linq; using System.Text; using Microsoft.Phone.UserData; + using System.IO; /// <summary> /// Implements helper functionality to serialize contact to JSON string. @@ -97,6 +98,7 @@ namespace WPCordovaClassLib.Cordova.Commands { "emails", () => string.Format("\"emails\":[{0}]", FormatJsonEmails(contact)) }, { "addresses", () => string.Format("\"addresses\":[{0}]", FormatJsonAddresses(contact)) }, { "urls", () => string.Format("\"urls\":[{0}]", FormatJsonWebsites(contact)) }, + { "photos", () => string.Format("\"photos\":[{0}]", FormatJsonPhotos(contact)) }, { "name", () => string.Format("\"name\":{0}", FormatJsonName(contact)) }, { "note", () => string.Format("\"note\":\"{0}\"", EscapeJson(contact.Notes.FirstOrDefault())) }, { @@ -288,5 +290,46 @@ namespace WPCordovaClassLib.Cordova.Commands return "{" + jsonAddress + "}"; } + + /// <summary> + /// Formats contact photos to JSON string. + /// </summary> + /// <param name="con">Contact object</param> + /// <returns>JSON string</returns> + private static string FormatJsonPhotos(Contact con) { + + // we return single photo since contact object instance contains single picture only + var photoStream = con.GetPicture(); + + if (photoStream == null) { + return ""; + } + + return String.Format("{{value:\"{0}\", type: \"data\", pref: false}}", GetImageContent(photoStream)); + } + + /// <summary> + /// Returns image content in a form of base64 string + /// </summary> + /// <param name="stream">Image stream</param> + /// <returns>Base64 representation of the image</returns> + private static string GetImageContent(Stream stream) + { + byte[] imageContent = null; + + try + { + int streamLength = (int)stream.Length; + imageContent = new byte[streamLength + 1]; + stream.Read(imageContent, 0, streamLength); + + } + finally + { + stream.Dispose(); + } + + return Convert.ToBase64String(imageContent); + } } } \ No newline at end of file
