In C# I am creating simple facebook application for WP7 and I came across a problem.
I'm trying to do the part where you can upload a picture in the album or feed.
Code:
FacebookMediaObject facebookUploader = new FacebookMediaObject { FileName = "SplashScreenImage.jpg", ContentType = "image/jpg" };
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~") + facebookUploader.FileNa开发者_Go百科me);
facebookUploader.SetValue(bytes);
Error:
- System.IO.File does not contain a definition for ReadAllBytes
You've got a couple of problems there. First off, Server.MapPath isn't going to give you the file location (since you're not in a web application). But once you do know the file path you're looking for (in IsolatedStorage), you can do something like this to read in the file as a byte array:
public byte[] ReadFile(String fileName)
{
byte[] bytes;
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream file = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
bytes = new byte[file.Length];
var count = 1024;
var read = file.Read(bytes, 0, count);
var blocks = 1;
while(read > 0)
{
read = file.Read(bytes, blocks * count, count);
blocks += 1;
}
}
}
return bytes;
}
I found a solution.
Code:
string imageName = boxPostImage.Text;
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);
IDictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("access_token", _AccessToken);
parameters.Add("source", fbUpload);
//_fbClient.PostAsync("/"+MainPage._albumId+"/photos", parameters);
_fbClient.PostAsync("/me/photos", parameters);
MessageBox.Show("Image has been posted successfully..");
}
catch (Exception error)
{
MessageBox.Show("Sorry, there's an error occured, please try again.");
}
精彩评论