Imran Mozumder
Imran Mozumder

Reputation: 268

How to upload images to facebook which is selected by using photoChooserTask in windows phone 8?

I am developing a Windows Phone app in which I have to post a photo to facebook. And that particular photo is choosen by using PhotoChooserTask or CameraChooserTask.

Normally, I can post a particular photo successfully, but I am facing problem to post the selected photo. I saw some link like link

So please if anyone know about the issue please help me out. Thanx in advance.

EDIT

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

On clicking on a button I am calling PostClicked class and it will directly go to facebook mainpage and it will ask for login information. Like this I am doing. Please check it out

Upvotes: 1

Views: 935

Answers (2)

Imran Mozumder
Imran Mozumder

Reputation: 268

Now I can share a photo to facebook successfully by using photochoosertask or cameratask. I am sharing my experience so that if anyone face the same issue can use it.

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

With this you can able to save the selected image to IsolatedStorage. And then at the time of posting the photo to facebook you have to select the image from 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....

Upvotes: 2

Sandeep Chauhan
Sandeep Chauhan

Reputation: 1313

you can do that by two methods :

1) by using mediasharetask in which it will show u all sharing account to which your phone is synced like facebook,gmail,linkdin,twitter,etc : it can be used like like this.

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

2) by using facebook sdk. you can get the package from nuget manager and then u can use it to share on facebook.

I hope this might help u.

Upvotes: 0

Related Questions