2015-04-18 64 views
1

我爲此創建了一個簡單頁面。 「選擇」button,「上傳」buttondisplayImage控件在從PhotoChooserTask中選擇後顯示圖像。以下是我的全班同學:上傳()請求不能在windows phone 8中使用Google Drive API C#

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");發生。但實際上圖像尚未上傳

回答

0

我剛剛找到了一個解決方案,這是非常簡單的,但讓我瞭解了一個月。

這不是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的路徑使用的方法,或者想尋找另一個字節的另一種方法[]方法工作。

希望它可以幫助別人。