Alex
Alex

Reputation: 350

Android Google Drive API download files from app folder

How can I download files on device from App Folder in Google Drive? 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:

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?

Update:

Maybe, I'm getting it wrong and there is no difference between downloading file content and downloading file?

Upvotes: 1

Views: 2917

Answers (2)

Moktahid Al Faisal
Moktahid Al Faisal

Reputation: 910

get all the data that you have saved in google drive by following the code

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

                    }
                });


    }

Upvotes: 0

abielita
abielita

Reputation: 13469

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:

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.

Example of performing a file download with 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.

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.

Upvotes: 2

Related Questions