[英]Upload() request not working with Google Drive API in windows phone 8 C#
我为此创建了一个简单页面。 从“ PhotoChooserTask
选择后,将显示一个“选择” button
,一个“上载” button
和一个displayImage
控件以显示图像。 下面是我的整个班级:
PhotoChooserTask photoChooserTask;
BitmapImage bmi;
// Constructor
public MainPage()
{
InitializeComponent();
}
private void chooseBtn_Click(object sender, RoutedEventArgs e)
{
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
photoChooserTask.Show();
}
private void photoChooserTask_Completed(object sender, PhotoResult e)
{
bmi = new BitmapImage();
bmi.SetSource(e.ChosenPhoto);
displayImage.Source = bmi;
}
private async void uploadBtn_Click(object sender, RoutedEventArgs e)
{
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile};
ClientSecrets secrets = new ClientSecrets()
{
ClientId = "MY CLIENT ID",
ClientSecret = "MY SECRET"
};
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(secrets,
scopes,
"user",
CancellationToken.None);
var initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Assignment 1",
};
//The authorization works!
var service = new DriveService(initializer);
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = "My document";
body.Description = "A test document";
body.MimeType = "image/jpeg";
byte[] byteArray = this.ConvertToBytes(bmi);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "image/jpeg");
MessageBox.Show("The code reached here"); // The app shows this message. But can't move to the Upload() line.
request.Upload();
MessageBox.Show("Upload completed!");
}
//The codes below is copied from http://stackoverflow.com/questions/4732807/conversion-of-bitmapimage-to-byte-array
public byte[] ConvertToBytes(BitmapImage bitmapImage)
{
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap
(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
Extensions.SaveJpeg(btmMap, ms,
bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
return ms.ToArray();
}
}
授权后,显示MessageBox.Show("The code reached here");
之后什么也没有发生MessageBox.Show("The code reached here");
就像完成工作一样没有错误。 但实际上图像尚未上传
我刚刚找到了一个非常简单的解决方案,但花了大约一个月的时间才弄清楚。
不是request.Upload();
,这是request.UploadAsync();
作为我的方法使用异步。
而且上面的ConvertToBytes
方法是错误的,我上传了一个黑色图像。 但是我尝试了另一种方法来获取路径字符串作为参数( ReadImageFile("myimage.jpg");
有效)
public static byte[] ReadImageFile(string imageLocation)
{
byte[] imageData = null;
FileInfo fileInfo = new FileInfo(imageLocation);
long imageFileLength = fileInfo.Length;
FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imageData = br.ReadBytes((int)imageFileLength);
return imageData;
}
我要做的下一件事是获取BitmapImage的路径以使用该方法,或者只是找到另一个有效的byte []方法。
希望它可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.