如何将多个音频文件上传到Google驱动器到驱动器中的指定文件夹?

How to Upload Multiple Audio Files to Google Drive To a Specified Folder in Drive?

我在一个项目中工作,该项目必须将多个音频文件 (.amr) 上传到 google 驱动器的特定文件夹。现在我正在通过 "startIntentSender" 方法来显示一个选择器来选择驱动器中的一个文件夹。对于单个文件,它工作正常,但我需要在没有任何选择器的情况下一次发送多个文件,我必须将其发送到我需要在程序中指定的特定文件夹。我搜索了很多,但我感到困惑。现在 Google 驱动器 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.");
                        }
                    }
                })

我建议你使用 google 驱动器 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

希望对您有所帮助。