上传具有Facebook状态的图像,WP8 [英] Upload Image with Facebook status, WP8

查看:107
本文介绍了上传具有Facebook状态的图像,WP8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过图像发送Facebook状态。找到代码,但它抛出
异常

I am unable to send the facebook status with image. Found the code but it throws the exception

安全句柄关闭

请允许我知道我是否缺少一些东西。
还有一个例子,其中使用了File.OpenRead(filename),但是它抛出了UnAuthorizedAccessException

kindly let me know if i am missing something. there was another example where File.OpenRead(filename) was used but it threw the 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();
            }
        }


推荐答案

发送通过媒体流播放Facebook状态的图像不是很好,因为它使用isolationStorage流造成麻烦。最好的方法是在Dictionary对象中移动参数,而不是匿名对象。所以共有2个变化。一个从FacebookMediaStream到FacebookMediaObject,第二个是使用Dictionary对象而不是使用匿名对象

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.

 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);
        }
    }

这篇关于上传具有Facebook状态的图像,WP8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆