I. Introduction
Việc chia sẻ hình ảnh qua Facebook là hết sức thông dụng trong các ứng dụng trên nền tảng điện thoại di động là hết sức thông dụng.
Ở Windows Phone 7 Microsoft chưa hổ trợ Luncher để làm được điều đó một cách dể dàng do đó chúng ta phải dùng Facebook API
Để bắt đầu bài này mình hy vọng các bạn đã đọc bài xác thực quyền truy cập vào facebook qua ứng dụng Windows Phone nhé
II. Fundamental
Ở bài trước mình đã chia sẻ cách đăng nhập vào Facebook và lấy token sau đó lưu thông tin vào IsolatedStorage.
Bây giờ trong trường hợp chúng ta đã xác thực được quyền truy cập vào Facebook và bây giờ muốn chia sẻ hình ảnh vào Facebook thì chúng ta phải load thông tin AccessToken từ IsolatedStorage từ đó mới tạo FacebookClient Object và gọi phương thức chia sẻ hình ảnh.
Trước tiên chúng ta làm một phương thức để lấy thông tin từ IsolatedStorage lên :
public static T LoadSetting(string fileName)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.FileExists(fileName))
return default(T);
try
{
using (var stream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
var serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(stream);
}
}
catch (Exception e)
{
Deployment.Current.Dispatcher.BeginInvoke(
() => MessageBox.Show(e.Message));
return default(T);
}
}
}
tiếp theo là gọi làm LoadSetting truyền vào đúng tên file mà bạn dùng để lưu trữ thông tin AccessToken trước đó và SharePhoto
var file = SerializeHelper.LoadSetting("FacebookAccess");
SharePhoto(file, message.Text);
Phương thức SharePhoto chúng ta truyền vào thông tin FacebookAccessToken và và đoạn Text mô tả cho hình ảnh mà chúng ta muốn Upload.
private void SharePhoto(FacebookSetting facebookSetting, string status)
{
try
{
var facebook = new Facebook.FacebookClient(facebookSetting.AccessToken);
facebook.PostCompleted += (o, args) =>
{
if (args.Error != null)
{
if (args.Error.Message.StartsWith("(OAuthException - #190)"))
{
Dispatcher.BeginInvoke(() =>
{
SetLoadingBar(false);
// Error 190 means that the token has expired. You need to ask the user to get the token again before continuing. Handle this error gracefully here.
// If you haven't AccessToken, you must navigate to LoginPage
var message = MessageBox.Show("Your token expired, you must signin agian. \nSignin now ???", "Hi my friend !", MessageBoxButton.OKCancel);
if (message == MessageBoxResult.OK)
NavigationService.Navigate(new Uri("/Pages/FacebookLoginPage.xaml", UriKind.Relative));
});
}
else
{
Dispatcher.BeginInvoke(() =>
{
SetLoadingBar(false);
MessageBox.Show(args.Error.Message, "Error", MessageBoxButton.OK);
});
}
return;
}
else
{
Dispatcher.BeginInvoke(() =>
{
SetLoadingBar(false);
MessageBox.Show("Your photos is now shared in Facebook", "Congratulations", MessageBoxButton.OK);
});
}
};
var facebookMediaObject = new Facebook.FacebookMediaObject()
{
FileName = "PhotosName.jpg",
ContentType = "image/jpg"
};
using (MemoryStream memoryStream = new MemoryStream())
{
WriteableBitmap writeableBitmap = new WriteableBitmap(App.ViewModel.CurrentImage);
writeableBitmap.SaveJpeg(memoryStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 95);
memoryStream.Seek(0, 0);
byte[] data = new byte[memoryStream.Length];
memoryStream.Read(data, 0, data.Length);
memoryStream.Close();
facebookMediaObject.SetValue(data);
}
//Hashtable hash = new Hashtable();
Dictionary<string, object> uploadData = new Dictionary<string, object>();
uploadData["message"] = App.ViewModel.CurrentTarget.Name;
uploadData["file"] = facebookMediaObject;
facebook.PostAsync("me/photos", uploadData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Trong phương thức SharePhoto chúng ta khởi tạo đối tượng FacebookClient và truyền vào những thông tin cần thiết như FileName, Photo Stream cuối cùng là gọi phương thức PostAsync để hoàn thành thao tác Upload.
Rất đơn giản chúng ta đã có thể Upload hình lên Facebook, giải pháp này mình đã dùng và upload hình ảnh cho ứng dụng V-Changed.
Chúc các bạn thành công nhé