I create a simple page for this. a "choose" button
, an "upload" button
and an displayImage
control to display the images after choosing from PhotoChooserTask
. And below is my whole 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 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();
}
}
After authorizing, nothing happens after showing MessageBox.Show("The code reached here");
with no error just like it's done its job. But actually the image has not been uploaded yet
I just found a solution for this which is very simple but got me about a month to figure it out.
It's not request.Upload();
, it's request.UploadAsync();
as my method use async.
And ConvertToBytes
method above is wrong, I uploaded a black image. But I tried another method which gets a string of path as an argument ( ReadImageFile("myimage.jpg");
works)
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;
}
The next things I'm gonna do is to get the path of my BitmapImage to use the method or just find another byte[] method that works.
Hope it helps someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.