【问题标题】:How do I upload a photo for a contact using the Google Contact .NET client library如何使用 Google Contact .NET 客户端库为联系人上传照片
【发布时间】:2009-03-06 18:01:07
【问题描述】:

所以在网上搜索之后,我拼凑了以下代码来为 Google 联系人上传照片:

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

服务正在使用 ClientLogin 身份验证。在这段代码运行之前,我已经成功检索到了联系人。

然而,在这段代码运行之后,我得到了一个 GDataRequestException。返回的错误消息是“发生了临时内部问题。请稍后再试”。 InnerException 中的错误消息包含以下内容,“远程服务器返回错误:(500) 内部服务器错误。”

经过更多研究,我发现来自 Google 服务的错​​误代码可能不是所有描述性的 - 并且可能只是返回错误 500。虽然我不知道这是多么真实。

所以问题可能出在我的代码上?或者更好的是,有人有示例代码来演示应该如何完成吗?

谢谢,

骆驼

【问题讨论】:

    标签: .net contacts gdata


    【解决方案1】:

    我找到了解决这个问题的两种方法(成绩单在this Google Contacts API post)。首先,您可以创建一个直接访问文件的流,如下所示:

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

    其次,我的原始代码失败的原因是因为我必须重置 MemoryStream 中的位置。这就是我在流媒体上如此菜鸟所得到的。一旦 Image.Save 函数完成,位置就在末尾。因此,您所要做的就是将位置重置为开头:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多