我正在一个项目中,必须将多个音频文件(.amr)上传到谷歌驱动器到特定文件夹。现在我正在通过“startIntentSender”方法来显示一个选择器来选择驱动器中的文件夹。对于单个文件,它工作正常,但我需要一次发送多个文件而没有任何选择器,我必须将它发送到我需要在程序中指定的特定文件夹。我搜索了很多,但我感到很困惑。现在推荐使用Google Drive Api。有多种上传方法吗?
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Failed to create new contents.");
return;
}
OutputStream outputStream = result.getDriveContents().getOutputStream();
try {
FileInputStream fileInputStream = new FileInputStream(mAllCallRecordFiles.get(i).getFilePath());
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e1) {
Log.i(TAG, "U AR A MORON! Unable to write file contents.");
}
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("audio/*")
.build();
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
try {
startIntentSender(intentSender, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "Failed to launch file chooser.");
}
}
})
我建议你使用谷歌驱动器api的REST部分来做到这一点:https://developers.google.com/drive/api/v2/about-sdk
看起来没有办法在单个请求中发送多个文件,但是您可以只使用一个文件进行循环并发送多个请求,然后您将同时上传它们。这可以帮助你:https://developers.google.com/drive/api/v2/manage-uploads
这样,您可以选择要在文件的父属性中设置正确ID的文件夹,如api文档中所述:
String folderId = "0BwwA4oUTeiV1TGRPeTVjaWRDY1E";
File fileMetadata = new File();
fileMetadata.setTitle("photo.jpg");
fileMetadata.setParents(Collections.singletonList(
new ParentReference().setId(folderId)));
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().insert(fileMetadata, mediaContent)
.setFields("id, parents")
.execute();
System.out.println("File ID: " + file.getId());
这是有关如何管理文件夹https://developers.google.com/drive/api/v2/folder的完整文档
我希望这可以帮助你。