在C#中,我正在为WP7创建简单的facebook应用程序,我遇到了一个问题。
我试着做的一部分,你可以上传一张照片在相册或饲料。
代码:
FacebookMediaObject facebookUploader = new FacebookMediaObject { FileName = "SplashScreenImage.jpg", ContentType = "image/jpg" };
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~") + facebookUploader.FileName);
facebookUploader.SetValue(bytes);错误:
的定义。
发布于 2011-06-28 14:07:56
我找到了解决办法。
代码:
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.");
}发布于 2011-06-28 06:05:40
你在那有几个问题。首先,Server.MapPath不会给出文件的位置(因为您不在web应用程序中)。但是,一旦您知道了要查找的文件路径(在IsolatedStorage中),就可以执行如下操作,将文件中的数据读入字节数组:
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;
}https://stackoverflow.com/questions/6502174
复制相似问题