2013-05-10 108 views
0

我正在开发使用facebook c#sdk的Windows Phone应用程序。我在将照片上传到Facebook墙上时遇到问题。 “var imageStream = File.OpenRead(imagepath);”行发生异常。我在这里做错了什么?DirectoryNotFoundException - 将照片上传到Facebook时

void cameraCaptureTask_Completed(object sender, PhotoResult e) 
    { 
     if (e.TaskResult == TaskResult.OK) 
     { 
      Uri uri = new Uri(e.OriginalFileName, UriKind.RelativeOrAbsolute); 
      //Code to display the photo on the page in an image control named myImage. 
      WriteableBitmap bmp = PictureDecoder.DecodeJpeg(e.ChosenPhoto); 

      myImage.Source = bmp; 

      imageSource = this.SaveImageToLocalStorage(bmp,System.IO.Path.GetFileName(e.OriginalFileName)); 
     } 
    } 

    private string SaveImageToLocalStorage(WriteableBitmap image, string imageFileName) 
    { 

     if (image == null) 
     { 
      throw new ArgumentNullException("imageBytes"); 
     } 
     try 
     { 
      var isoFile = IsolatedStorageFile.GetUserStoreForApplication(); 

      if (!isoFile.DirectoryExists("MyPhotos")) 
      { 
       isoFile.CreateDirectory("MyPhotos"); 
      } 

      string filePath = System.IO.Path.Combine("/" + "MyPhotos" + "/", imageFileName); 
      using (var stream = isoFile.CreateFile(filePath)) 
      { 
       image.SaveJpeg(stream, image.PixelWidth, image.PixelHeight, 0, 80); 
      } 

      return new Uri(filePath, UriKind.Relative).ToString(); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 


    private void ApplicationBarIconButton_Click(object sender, EventArgs e) 
    { 
     postImage(App.AccessToken,titleBox.Text + descriptionBox.Text, imageSource); 
    } 

    private void postImage(string p1, string p2, string imagepath) 
    { 
     FacebookClient fb = new FacebookClient(p1); 
     var imageStream = File.OpenRead(imagepath); 
     dynamic res = fb.PostTaskAsync("/me/photos", new 
     { 
      message = p2, 
      file = new FacebookMediaStream 
      { 
       ContentType = "image/jpg", 
       FileName = Path.GetFileName(imagepath) 
      }.SetValue(imageStream) 
     }); 

    } 

回答

0

你不应该通过一个正斜杠(/)到Path.CombinePath.Combine为你增加了乐趣。

string filePath = System.IO.Path.Combine("MyPhotos", imageFileName); 

这将使正确的正确路径:MyPhotos /映像文件名称在您的原始代码做出错误的路径:/映像文件名称

编辑:

postImage()方法使用IsolatedStorageFile的的OpenFile方法来代替File.OpenRead。

var isf = IsolatedStorageFile.GetUserStoreForApplication(); 
var imageStream = isf.OpenFile(imagepath, FileMode.Open); 
+0

我改变..但它仍然显示了同样的异常... :( – 2013-05-10 18:01:25

+0

什么是你传递给postImage()中的ImagePath参数? – 2013-05-10 18:10:15

+0

由线返回的图像的路径'返回新的URI( filePath,UriKind.Relative).ToString' – 2013-05-10 18:17:17