2012-12-27 62 views
0

我無法通過圖片發送Facebook狀態。發現的代碼,但它拋出的異常 使用Facebook狀態上傳圖片,WP8

The Safe handle is closed

請讓我知道,如果我失去了一些東西。 有其中使用File.OpenRead(文件名)的另一例子,但它投擲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(); 
      } 
     } 
+0

標記哪種編程語言? –

回答

1

通過媒體流發送圖像與facebook狀態是不好的,因爲它造成麻煩與isolatedStorage流。最好的方法是在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); 
     } 
    }