Android Google Drive API download files from app folder
up vote
0
down vote
favorite
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?
add a comment |
up vote
0
down vote
favorite
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?
1
You cannot "download those files to a specific folder on device" using Drive. What you can do though, is create a new file on the device by copying the content you get from Drive. You'll have to create the file on the device yourself.
– Anatoli
Jun 10 '16 at 3:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
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?
edited Jun 9 '16 at 13:25
asked Jun 9 '16 at 13:06
Alex
112113
112113
1
You cannot "download those files to a specific folder on device" using Drive. What you can do though, is create a new file on the device by copying the content you get from Drive. You'll have to create the file on the device yourself.
– Anatoli
Jun 10 '16 at 3:54
add a comment |
1
You cannot "download those files to a specific folder on device" using Drive. What you can do though, is create a new file on the device by copying the content you get from Drive. You'll have to create the file on the device yourself.
– Anatoli
Jun 10 '16 at 3:54
1
1
You cannot "download those files to a specific folder on device" using Drive. What you can do though, is create a new file on the device by copying the content you get from Drive. You'll have to create the file on the device yourself.
– Anatoli
Jun 10 '16 at 3:54
You cannot "download those files to a specific folder on device" using Drive. What you can do though, is create a new file on the device by copying the content you get from Drive. You'll have to create the file on the device yourself.
– Anatoli
Jun 10 '16 at 3:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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.metadatascope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting theviewersCanCopyContentfield 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.
So, this method uses Drive REST API, not Drive API for Android am I correct?
– Alex
Jun 13 '16 at 9:29
add a comment |
up vote
0
down vote
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) {
}
});
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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.metadatascope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting theviewersCanCopyContentfield 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.
So, this method uses Drive REST API, not Drive API for Android am I correct?
– Alex
Jun 13 '16 at 9:29
add a comment |
up vote
1
down vote
accepted
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.metadatascope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting theviewersCanCopyContentfield 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.
So, this method uses Drive REST API, not Drive API for Android am I correct?
– Alex
Jun 13 '16 at 9:29
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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.metadatascope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting theviewersCanCopyContentfield 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.
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.metadatascope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting theviewersCanCopyContentfield 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.
edited May 23 '17 at 11:58
Community♦
11
11
answered Jun 10 '16 at 8:36
abielita
9,0152839
9,0152839
So, this method uses Drive REST API, not Drive API for Android am I correct?
– Alex
Jun 13 '16 at 9:29
add a comment |
So, this method uses Drive REST API, not Drive API for Android am I correct?
– Alex
Jun 13 '16 at 9:29
So, this method uses Drive REST API, not Drive API for Android am I correct?
– Alex
Jun 13 '16 at 9:29
So, this method uses Drive REST API, not Drive API for Android am I correct?
– Alex
Jun 13 '16 at 9:29
add a comment |
up vote
0
down vote
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) {
}
});
}
add a comment |
up vote
0
down vote
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) {
}
});
}
add a comment |
up vote
0
down vote
up vote
0
down vote
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) {
}
});
}
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) {
}
});
}
answered Nov 10 at 21:46
Chayon Ahmed
456613
456613
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37726870%2fandroid-google-drive-api-download-files-from-app-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You cannot "download those files to a specific folder on device" using Drive. What you can do though, is create a new file on the device by copying the content you get from Drive. You'll have to create the file on the device yourself.
– Anatoli
Jun 10 '16 at 3:54