1

我正在使用适用于 iPhone 的 Google Drive SDK 并尝试在“TestAudio”文件夹中上传音频文件。如果“TestAudio”文件夹未在 google drive 中创建,则首先创建该文件夹,然后我的音频应仅存储到该文件夹​​。除了文件夹创建之外,一切都在工作 gr8。有朋友可以帮忙吗?

我正在使用以下代码上传音频文件。

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];
}
}];
4

1 回答 1

1

我没有看到您创建文件夹的代码,但我自己在创建文件夹时遇到了同样的问题。如您所知,mimeType 必须是“application/vnd.google-apps.folder”。如果 uploadParametersWithData 的 NSData 参数为零,我会遇到断言失败。所以我尝试了一个零长度的 NSData 对象,但失败了。使用 1 字节的 NSData 对象也失败了。诀窍是使用 uploadParameters:nil 调用 queryForFilesUpdateWithObject。然后文件夹创建工作正常。我还发现最后显示的 Objective-C 代码:

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

是不正确的。file.parents 应如下所示:

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = parentID;
if (parentID.length>0) file.parents = [NSArray arrayWithObjects:parentRef,nil];
于 2013-02-13T23:15:24.590 回答