Android Google Drive API 从应用程序文件夹下载文件

Android Google Drive API download files from app folder

如何从 Google 驱动器中的 App Folder 下载设备上的文件?此文件夹对用户不可见,只有特定的应用程序才能使用它。

基本上,我需要在此文件夹中上传多个文件(一些应用程序和用户设置),然后我想将其下载回设备上。它的工作方式类似于某种用户数据备份。

我已经在 App 文件夹中创建了文件,我可以这样阅读它们:

DriveFile appFolderFile = driveApi.getFile(googleApiClient, driveId);

但我不知道如何上传现有文件,然后将这些文件下载到设备上的特定文件夹。我搜索了文档,但没有找到解决方案。

在文档中我找到了如何读取和检索文件内容,但没有关于下载文件本身的信息。

任何人都可以提示我如何操作吗?或者,也许,我只是错过了文档中的正确部分,或者它甚至是不可能的,我必须使用 REST API?

更新:

可能是我理解错了,下载文件内容和下载文件没有区别?

download files, you make an authorized HTTP GET request to the file's resource URL 并包含查询参数 alt=media,如下所示:

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs

Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content. For example, an app using the drive.readonly.metadata scope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true.

使用云端硬盘 API 执行文件下载的示例:

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
        .executeMediaAndDownloadTo(outputStream);

下载完成后,您需要使用parent 参数将文件放入特定文件夹,然后在文件的parents 属性 中指定正确的ID。

示例:

String folderId = "0BwwA4oUTeiV1TGRPeTVjaWRDY1E";
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
fileMetadata.setParents(Collections.singletonList(folderId));
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
        .setFields("id, parents")
        .execute();
System.out.println("File ID: " + file.getId());

检查此 thread

按照代码

获取您保存在 google 驱动器中的所有数据
 public void retrieveContents(DriveFile file) {

        Task<DriveContents> openFileTask =
                getDriveResourceClient().openFile(file, DriveFile.MODE_READ_ONLY);



        openFileTask.continueWithTask(new Continuation<DriveContents, Task<Void>>() {
            @Override
            public Task<Void> then(@NonNull Task<DriveContents> task) throws Exception {
                DriveContents contents = task.getResult();

                try (BufferedReader reader = new BufferedReader(
                        new InputStreamReader(contents.getInputStream()))) {
                    StringBuilder builder = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line).append("\n");
                    }

                    Log.e("result ", builder.toString());
                }

                Task<Void> discardTask = MainActivity.this.getDriveResourceClient().discardContents(contents);

                return discardTask;
            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });


    }