2012-12-14 118 views
1

我正在使用適用於iPhone的Google Drive SDK,並嘗試在「TestAudio」文件夾中上傳音頻文件。如果「TestAudio」文件夾未在Google Drive中創建,請首先創建該文件夾,然後我的音頻應只存儲到該文件夾​​。除了文件夾創建,每件事情都在gr8上工作。可以任何哥們請幫忙嗎?如何使用Google Drive SDK for iPhone在Google雲端硬盤上創建文件夾?

我使用下面的代碼上傳音頻文件。

GTLUploadParameters *uploadParameters = nil; 

NSString *soundFilePath = [[NSBundle mainBundle] 
          pathForResource:@"honey_bunny_new" 
          ofType:@"mp3"]; 

if (soundFilePath) { 
    NSData *fileContent = [[NSData alloc] initWithContentsOfFile:soundFilePath]; 
    uploadParameters = [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"audio/mpeg"]; 
} 

self.driveFile.title = self.updatedTitle; 
GTLQueryDrive *query = nil; 
if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0) { 
    // This is a new file, instantiate an insert query. 
    query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile 
             uploadParameters:uploadParameters]; 
} else { 
    // This file already exists, instantiate an update query. 
    query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile 
                fileId:self.driveFile.identifier 
             uploadParameters:uploadParameters]; 
} 
UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"Saving file" 
                 delegate:self]; 

[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, 
                  GTLDriveFile *updatedFile, 
                  NSError *error) { 
[alert dismissWithClickedButtonIndex:0 animated:YES]; 
if (error == nil) { 
    self.driveFile = updatedFile; 
    self.originalContent = [self.textView.text copy]; 
    self.updatedTitle = [updatedFile.title copy]; 
    [self toggleSaveButton]; 
    [self.delegate didUpdateFileWithIndex:self.fileIndex 
            driveFile:self.driveFile]; 
    [self doneEditing:nil]; 
} else { 
    NSLog(@"An error occurred: %@", error); 
    [DrEditUtilities showErrorMessageWithTitle:@"Unable to save file" 
              message:error.description 
              delegate:self]; 
} 
}]; 

回答

1

我沒有看到你的代碼來創建一個文件夾,但我自己也有與創建文件夾相同的問題。如您所知,mimeType必須爲「application/vnd.google-apps.folder」。如果uploadParametersWithData的NSData參數爲零,我會遇到斷言失敗。所以我嘗試了一個零長度的NSData對象,並失敗了。使用1個字節的NSData對象也失敗了。訣竅是使用uploadParameters調用queryForFilesUpdateWithObject:nil。然後,文件夾創建工作正常。我還發現,顯示在末端的Objective-C代碼:

https://developers.google.com/drive/v2/reference/files/insert

不正確。該文件的父母應如下:

GTLDriveParentReference *parentRef = [GTLDriveParentReference object]; 
parentRef.identifier = parentID; 
if (parentID.length>0) file.parents = [NSArray arrayWithObjects:parentRef,nil]; 
+0

如何獲得parentID? – Yashesh

相關問題