Upload() 请求不适用于 Google Drive API in windows phone 8 C#
Upload() request not working with Google Drive API in windows phone 8 C#
我为此创建了一个简单的页面。 "choose" button
、"upload" button
和 displayImage
控件,用于在从 PhotoChooserTask
中选择后显示图像。下面是我的全部 class:
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
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");
后没有任何错误,就像它完成了它的工作一样。但实际上图片还没有上传
我刚刚找到了一个非常简单的解决方案,但我花了大约一个月的时间才弄明白。
不是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[] 方法。
希望对大家有所帮助。
我为此创建了一个简单的页面。 "choose" button
、"upload" button
和 displayImage
控件,用于在从 PhotoChooserTask
中选择后显示图像。下面是我的全部 class:
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
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");
后没有任何错误,就像它完成了它的工作一样。但实际上图片还没有上传
我刚刚找到了一个非常简单的解决方案,但我花了大约一个月的时间才弄明白。
不是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[] 方法。
希望对大家有所帮助。