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?










share|improve this question




















  • 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

















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?










share|improve this question




















  • 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















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?










share|improve this question















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?







android google-drive-android-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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














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






share|improve this answer























  • So, this method uses Drive REST API, not Drive API for Android am I correct?
    – Alex
    Jun 13 '16 at 9:29


















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

}
});


}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























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






    share|improve this answer























    • So, this method uses Drive REST API, not Drive API for Android am I correct?
      – Alex
      Jun 13 '16 at 9:29















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






    share|improve this answer























    • So, this method uses Drive REST API, not Drive API for Android am I correct?
      – Alex
      Jun 13 '16 at 9:29













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






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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












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

    }
    });


    }





    share|improve this answer

























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

      }
      });


      }





      share|improve this answer























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

        }
        });


        }





        share|improve this answer












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

        }
        });


        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 21:46









        Chayon Ahmed

        456613




        456613






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."