我正在使用facebook c#sdk开发Windows手机应用程序。我在将照片上传到Facebook墙时遇到问题。 “var imageStream = File.OpenRead(imagepath);”行中发生异常。我在这里做错了什么?
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 :(得分:0)
您不应将正斜杠( / )传递给Path.Combine
。 Path.Combine
为你增加了灵感。
string filePath = System.IO.Path.Combine("MyPhotos", imageFileName);
这将使正确的路径正确:MyPhotos / imageFileName您的原始代码出错路径:/ imageFileName
修改强>
在postImage()
方法中,使用IsolatedStorageFile的OpenFile方法而不是File.OpenRead。
var isf = IsolatedStorageFile.GetUserStoreForApplication();
var imageStream = isf.OpenFile(imagepath, FileMode.Open);