0

我无法用图片发送 Facebook 状态。找到代码但它抛出异常

The Safe handle is closed

如果我遗漏了什么,请告诉我。还有另一个使用 File.OpenRead(filename) 的例子,但它抛出了 UnAuthorizedAccessException

代码如下:

public static Stream ImageToShare
        {
        set
            {
                try
                {
                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (myIsolatedStorage.FileExists(ImageToShareKey))
                            myIsolatedStorage.DeleteFile(ImageToShareKey);

                        IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(ImageToShareKey);

                        StreamResourceInfo sri = null;
                        Uri uri = new Uri(ImageToShareKey, UriKind.Relative);
                        sri = Application.GetResourceStream(uri);

                        BitmapImage bitmap = new BitmapImage();
                        bitmap.SetSource(value);
                        //bitmap.SetSource(sri.Stream);
                        WriteableBitmap wb = new WriteableBitmap(bitmap);

                        Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                        fileStream.Close();
                    }
                }
                catch (Exception ex)
                {
                    AppHelper.ErrorOccured(ex);
                }
            }

private void postFBWithImage()
        {
            try
            {

                using (IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication())
                using (IsolatedStorageFileStream stream = myFile.OpenFile("SharePhoto", FileMode.Open, FileAccess.Read))
                using (var imgFile = new FacebookMediaStream
                {
                    ContentType = "image/jpeg",
                    FileName = Path.GetFileName(imgPath.Text),
                }.SetValue(stream))
                {

                    var fb = new FacebookClient(AppSettings.FacebookPIN);
                    fb.PostCompleted += (o, args) =>
                    {
                        if (args.Error != null)
                        {
                            notPosted(args);
                            return;
                        }

                        Dispatcher.BeginInvoke(() =>
                        {
                            posted();
                        });
                    };

                    fb.PostAsync("me/photos", new { message = ShareComments, imgFile });
                }
            }
            catch (Exception ex)
            {
                AppHelper.ErrorOccured(ex);
                postFBWithoutImage();
            }
        }
4

1 回答 1

1

通过媒体流发送带有 facebook 状态的图像并不好,因为它会在隔离存储流中产生问题。最好的方法是在 Dictionary 对象而不是匿名对象中移动参数。所以总共有2个变化。一是从 FacebookMediaStream 到 FacebookMediaObject,二是使用 Dictionary 对象而不是使用匿名对象

以下是描述场景的代码。

 private void postFBWithImage()
    {
        try
        {
            using (IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication())
            using (IsolatedStorageFileStream stream = myFile.OpenFile(AppSettings.ImageToShareKey, FileMode.Open, FileAccess.Read))
            {
                byte[] byteArr = null;
                using (var binRdr = new BinaryReader(stream))
                using (var memStr = new MemoryStream())
                {
                    const int ReadSize = 8196;
                    int chunkSize = 0;
                    do
                    {
                        byte[] buf = new byte[ReadSize];
                        chunkSize = binRdr.Read(buf, 0, ReadSize);
                        memStr.Write(buf, 0, ReadSize);
                    } while (chunkSize > 0);

                    byteArr = new byte[memStr.Length];
                    memStr.Position = 0;
                    memStr.Read(byteArr, 0, (int)memStr.Length);
                }

                var fb = new FacebookClient(AppSettings.FacebookPIN);
                fb.PostCompleted += (o, args) =>
                {
                    if (args.Error != null)
                    {
                        notPosted(args);
                        return;
                    }

                    Dispatcher.BeginInvoke(() =>
                    {
                        posted();
                    });
                };

                var parameters = new Dictionary<string, object>();
                parameters["message"] = ShareComments;
                parameters["file"] = new FacebookMediaObject
                    {
                        ContentType = "image/jpeg",
                        FileName = "image.jpeg",
                    }.SetValue(byteArr);
                fb.PostAsync("me/photos", parameters);

            }
        }
        catch (Exception ex)
        {
            AppHelper.ErrorOccured(ex);
        }
    }
于 2013-01-06T13:09:50.213 回答