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

时间:2019-04-12 10:50:37

标签: android audio google-drive-android-api

我正在一个项目中工作,该项目必须将多个音频文件(.amr)上传到Google驱动器到特定的文件夹。现在,我将通过“ 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.");
                        }
                    }
                })

1 个答案:

答案 0 :(得分:1)

我建议您使用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

的完整文档。

希望对您有帮助。