Tutorial :How do I upload a photo for a contact using the Google Contact .NET client library



Question:

So after scouring the Internet I pieced together the following code to upload a photo for a Google Contact:

System.Drawing.Bitmap Image = new Bitmap( @"C:\test.jpg" );  System.IO.MemoryStream Memory = new MemoryStream();  Image.Save( Memory, System.Drawing.Imaging.ImageFormat.Jpeg );    Service.Update      (          Contact.PhotoEditUri,           Memory,           "image/jpeg",           null      );  

The Service is using ClientLogin authentication. Before this code runs, I have successfully retrieved the contact.

After this code runs, however, I get a GDataRequestException. The error message returned is, "A temporary internal problem has occurred. Try again later". The error message in the InnerException contains the following, "The remote server returned an error: (500) Internal Server Error."

After yet more research, I found hints that the error codes from the Google service may not be all that descriptive - and may simply return error 500. Though I don't know how true this is.

So maybe the problem is with my code? Or even better, does anybody have sample code to demonstrate how this should be done?

Thanks,

Camel


Solution:1

I found out two ways to resolve this problem (the transcript is in this Google Contacts API post). First, you can create a stream that accesses the file directly, like so:

System.IO.FileStream File = new FileStream( @"C:\test.jpg" );    Service.Update      (          Contact.PhotoEditUri,           File,           "image/jpeg",           null      );  

Second, the reason why my original code failed was because I had to reset the position inside the MemoryStream. This is what I get for being such a noob on streams. Once the Image.Save function completes, the position is at the end. So, all you have to do is reset the position to the beginning:

System.Drawing.Bitmap Image = new Bitmap( @"C:\test.jpg" );  System.IO.MemoryStream Memory = new MemoryStream();  Image.Save( Memory, System.Drawing.Imaging.ImageFormat.Jpeg );  Memory.Seek( 0, SeekOrigin.Begin );    Service.Update      (          Contact.PhotoEditUri,           Memory,           "image/jpeg",           null      );  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »