繁体   English   中英

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

[英]Android Google Drive API download files from app folder

如何从Google云端硬盘中的“ 应用程序文件夹”下载设备上的文件 该文件夹对用户不可见,只有特定的应用程序可以使用它。

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

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

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

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

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

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

更新:

也许我弄错了,下载文件内容和下载文件之间没有区别?

下载文件 ,请向文件的资源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());

检查该线程

按照以下代码获取已保存在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) {

                    }
                });


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM