Search code examples
androidgoogle-drive-android-api

Download file from Google Drive in android


I followed this tutorial in my android app, and I can login, select, view file from my Google Drive account. How can I download a file to my cache folder when I know the DriveId of the file. I know I can obtain drivefile like this.

DriveFile driveFile = mFileId.asDriveFile();

Can I convert this driveFile to File. I tried to cast it, didn't work. In this link there is a method for downloading the file, but i can't find files() or executeMediaAndDownloadTo in my Drive driveService. Is this for an old version? I couldn't find anything useful on the internet for this.


Solution

  • Try

    DriveApi.DriveContentsResult result = id.asDriveFile()
                      .open(client, DriveFile.MODE_READ_ONLY, null).await();
    

    check status is good with

    result.getStatus().isSuccess()
    

    then access the file with

    result.getDriveContents().getInputStream() 
    

    client is your GoogleApiClient instance

    Write the InputStream to a File if you want to create a copy of the file.