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?
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 theviewersCanCopyContent
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.