0

我正在使用 facebook c# sdk 开发一个 windows phone 应用程序。我在将照片上传到 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)
        });

    }
4

1 回答 1

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);
于 2013-05-10T16:15:08.340 回答