I am unable to send the facebook status with image. Found the code but it throws the
exception
The Safe handle is closed
kindly let me know if i am missing something.
there was another example where File.OpenRead(filename) was used but it threw the UnAuthorizedAccessException
the code is following :
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();
}
}
sending image with facebook status through media stream is not good as it creates trouble with isolatedStorage streams. best way is to move parameters in Dictionary object rather than anonymous objects. so there are totally 2 changes. one from FacebookMediaStream to FacebookMediaObject and second is the use of Dictionary object instead of using anonymous object
following is the code to describe the scenario.