简体   繁体   English

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

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

How can I download files on device from App Folder in Google Drive? 如何从Google云端硬盘中的“ 应用程序文件夹”下载设备上的文件 This folder is invisible for user and only specific application can use it. 该文件夹对用户不可见,只有特定的应用程序可以使用它。

Basically, I need to upload multiple files inside this folder (some application and user settings) and then I want to download it back on device. 基本上,我需要在此文件夹中上传多个文件(某些应用程序和用户设置),然后再将其重新下载到设备上。 It works like some kind of back up for user data. 它就像某种形式的用户数据备份。

I've done creating files inside App Folder and I can read them like this: 我已经在App Folder中创建了文件,我可以这样读取它们:

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

But I don't know how can I upload existing files and then download those files to a specific folder on device. 但是我不知道如何上传现有文件,然后将那些文件下载到设备上的特定文件夹中。 I've searched documentation, but found no solution. 我搜索了文档,但没有找到解决方案。

In documentation I've found how to read and retrieve file content, but no information about downloading the file itself. 在文档中,我发现了如何读取和检索文件内容,但是没有有关下载文件本身的信息。

Can anybody give me a hint on how to do it? 有人可以给我提示如何做吗? Or maybe, I just missed a correct section in documentation or it's even impossible and I have to use REST API? 或者,也许我只是错过了文档中的正确部分,或者甚至不可能,而我必须使用REST API?

Update: 更新:

Maybe, I'm getting it wrong and there is no difference between downloading file content and downloading file? 也许我弄错了,下载文件内容和下载文件之间没有区别?

To download files , you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media like this: 下载文件 ,请向文件的资源URL发出授权的HTTP GET请求,并包含查询参数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. 例如,使用drive.readonly.metadata范围的应用将无权下载文件内容。 Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true. 具有编辑权限的用户可以通过将viewersCanCopyContent字段设置为true来限制只读用户的下载。

Example of performing a file download with Drive API: 使用Drive API执行文件下载的示例:

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

Once you have downloaded, you need to use the parent parameter to put a file in a specific folder then specify the correct ID in the parents property of the file. 下载后,需要使用parent参数将文件放入特定文件夹,然后在文件的parents属性中指定正确的ID。

Example: 例:

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());

Check this thread . 检查该线程

get all the data that you have saved in google drive by following the code 按照以下代码获取已保存在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