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

标签 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.");
                        }
                    }
                })

最佳答案

我建议您使用Google Drive 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的完整文档。

我希望这可以帮助你。

关于android - 如何将多个音频文件上传到Google Drive到Drive中的指定文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649983/

相关文章:

android - 如果我使用 Google Play 服务,用户是否需要登录 Google Play?

android - 解码器的输出表面如何传递到编码器的输入表面?

android - 使用 Drive sdk 从 Drive 文件夹访问完整的文件列表

android - Google Drive Api 已弃用(Google 玩游戏 -> 保存的游戏)

android - 通过共享链接从 Google Drive 获取文件

android - Android 中的无限列表

java - 如何从设备电话簿中获取所有联系人

android:附加音频

audio - FFMPEG:我可以将音频设置为仅在晚上播放吗?

c++ - 在网络浏览器中运行 C/C++ 代码?