如何将图像上传到Facebook,这是通过在Windows Phone 8中使用photoChooserTask选择的?

时间:2013-08-26 09:50:05

标签: windows-phone-7 c#-4.0 windows-phone-8 windows-phone

我正在开发一个Windows Phone应用程序,我必须将照片发布到Facebook。并且使用PhotoChooserTask或CameraChooserTask选择该特定照片。

通常情况下,我可以成功发布特定照片,但发布所选照片时遇到问题。我看到了一些链接 link

所以,如果有人知道这个问题,请帮助我。 提前完成。

修改

private void PostClicked(object sender, RoutedEventArgs e)
    {
        //Client Parameters
        var parameters = new Dictionary<string, object>();
        //var parameters1 = new Dictionary<>();
        parameters["client_id"] = FBApi;
        parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
        parameters["response_type"] = "token";
        parameters["display"] = "touch";
        parameters["ContentType"] = "image/png";
        //The scope is what give us the access to the users data, in this case
        //we just want to publish on his wall
        parameters["scope"] = "publish_stream";
        Browser.Visibility = System.Windows.Visibility.Visible;
        Browser.Navigate(client.GetLoginUrl(parameters));
    }
private void BrowserNavitaged(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookOAuthResult oauthResult;
        //Making sure that the url actually has the access token
        if (!client.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
        {
            return;
        }
        //Checking that the user successfully accepted our app, otherwise just show the error
        if (oauthResult.IsSuccess)
        {
            //Process result
            client.AccessToken = oauthResult.AccessToken;
            //Hide the browser
            Browser.Visibility = System.Windows.Visibility.Collapsed;
            PostToWall();
        }
        else
        {
            //Process Error
            MessageBox.Show(oauthResult.ErrorDescription);
            Browser.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
private void PostToWall()
    {
        string imageName = "ic_launcher.png";
        StreamResourceInfo sri = null;
        Uri jpegUri = new Uri(imageName, UriKind.Relative);
        sri = Application.GetResourceStream(jpegUri);
        try
        {
            byte[] imageData = new byte[sri.Stream.Length];
            sri.Stream.Read(imageData, 0, System.Convert.ToInt32(sri.Stream.Length));
            FacebookMediaObject fbUpload = new FacebookMediaObject
            {
                FileName = imageName,
                ContentType = "image/jpg"
            };
            fbUpload.SetValue(imageData);
            string name1 = eventname.Text;
            string format = "yyyy-MM-dd";
            string message1 = eventmessage.Text;
            string date1 = datepicker.ValueString;
            DateTime datevalue = DateTime.Parse(date1);
            string d = datevalue.ToString(format);
            string memoType = "Tribute";
            var parameters = new Dictionary<string, object>();
            var parameters1 = new Dictionary<string, object>();
            parameters["message"] = name1 + "\n" + d + "\n" + memoType + "\n" + message1;
            parameters["source"] = fbUpload;

            webservice();
            client.PostTaskAsync("me/photos", parameters);
        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }

        //client.PostTaskAsync("me/photos", parameters1);
    }

点击按钮我正在调用PostClicked类,它将直接转到facebook主页,它会询问登录信息。像这样我正在做。 请检查出来

2 个答案:

答案 0 :(得分:2)

现在我可以使用photochoosertask或cameratask成功地将照片分享到facebook。 我正在分享我的经验,以便如果有人遇到同样的问题就可以使用它。

private void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        BitmapImage image = new BitmapImage();
        image.SetSource(e.ChosenPhoto);
        SaveImageToIsolatedStorage(image, tempJPEG);
        this.image.Source = image;
    }
public void SaveImageToIsolatedStorage(BitmapImage image, string fileName)
    {
        using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isolatedStorage.FileExists(fileName))
                isolatedStorage.DeleteFile(fileName);
            var fileStream = isolatedStorage.CreateFile(fileName);
            if (image != null)
            {
                var wb = new WriteableBitmap(image);
                wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
            }
            fileStream.Close();
        }
    }

使用此功能,您可以将所选图像保存到IsolatedStorage。 然后在将照片发布到Facebook时,您必须从IsolatedStorage中选择图像。

private void PostClicked(object sender, RoutedEventArgs e)
    {
        //Client Parameters
        var parameters = new Dictionary<string, object>();
        parameters["client_id"] = FBApi;
        parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
        parameters["response_type"] = "token";
        parameters["display"] = "touch";
        //The scope is what give us the access to the users data, in this case
        //we just want to publish on his wall
        parameters["scope"] = "publish_stream";
        Browser.Visibility = System.Windows.Visibility.Visible;
        Browser.Navigate(client.GetLoginUrl(parameters));
    }
private void BrowserNavitaged(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookOAuthResult oauthResult;
        //Making sure that the url actually has the access token
        if (!client.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
        {
            return;
        }
        //Checking that the user successfully accepted our app, otherwise just show the error
        if (oauthResult.IsSuccess)
        {
            //Process result
            client.AccessToken = oauthResult.AccessToken;
            //Hide the browser
            Browser.Visibility = System.Windows.Visibility.Collapsed;
            PostToWall();
        }
        else
        {
            //Process Error
            MessageBox.Show(oauthResult.ErrorDescription);
            Browser.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
private void PostToWall()
    {
        try
        {
            byte[] data;
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read))
                {
                    data = new byte[fileStream.Length];
                    fileStream.Read(data, 0, data.Length);
                    fileStream.Close();
                }
            }
            //MemoryStream ms = new MemoryStream(data);
            //BitmapImage bi = new BitmapImage();
            //// Set bitmap source to memory stream 
            //bi.SetSource(ms);
            //this.imageTribute.Source = bi;
            FacebookMediaObject fbUpload = new FacebookMediaObject
            {
                FileName = tempJPEG,
                ContentType = "image/jpg"
            };
            fbUpload.SetValue(data);
            string name1 = eventname.Text;
            string format = "yyyy-MM-dd";
            string message1 = eventmessage.Text;
            string date1 = datepicker.ValueString;
            DateTime datevalue = DateTime.Parse(date1);
            string d = datevalue.ToString(format);
            string memoType = "Notice";
            var parameters = new Dictionary<string, object>();
            var parameters1 = new Dictionary<string, object>();
            parameters["message"] = name1;
            parameters["source"] = fbUpload;
            webservice();
            client.PostTaskAsync("me/photos", parameters);

        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }
    }

Thanx to all ....

答案 1 :(得分:0)

你可以通过两种方法来做到这一点:

1)通过使用mediasharetask,它将显示你的手机同步的所有共享帐户,如facebook,gmail,linkdin,twitter等:它可以像这样使用。

           ShareMediaTask shareMediaTask = new ShareMediaTask();
           shareMediaTask.FilePath = path;
           shareMediaTask.Show();

2)使用facebook sdk。你可以从nuget经理那里得到包裹然后你可以用它在facebook上分享。

我希望这可能有助于你。