【问题标题】:Android Google Drive API download files from app folderAndroid Google Drive API 从应用文件夹下载文件
【发布时间】:2016-06-09 13:06:06
【问题描述】:

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

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

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

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

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

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

有人可以给我一个提示吗?或者,我只是错过了文档中的正确部分,或者这甚至是不可能的,我必须使用 REST API?

更新:

也许,我理解错了,下载文件内容和下载文件没有区别?

【问题讨论】:

  • 您无法使用云端硬盘“将这些文件下载到设备上的特定文件夹”。不过,您可以做的是通过复制从云端硬盘获取的内容在设备上创建一个新文件。您必须自己在设备上创建文件。

标签: android google-drive-android-api


【解决方案1】:

download files,您向文件的resource URL 发出授权的HTTP GET 请求,并包含查询参数alt=media,如下所示:

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

下载文件需要用户至少具有读取权限。此外,您的应用程序必须获得允许读取文件内容的范围的授权。例如,使用drive.readonly.metadata 范围的应用将无权下载文件内容。拥有编辑权限的用户可以通过将viewersCanCopyContent字段设置为true来限制只读用户的下载。

使用 Drive 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

【讨论】:

  • 那么,这个方法使用 Drive REST API,而不是适用于 Android 的 Drive API 对吗?
【解决方案2】:

按照代码获取您保存在谷歌驱动器中的所有数据

 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) {

                    }
                });


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多