Search code examples
javaandroidgoogle-classroom

Finding a way to download course materials to android mobile


Is there any way to download google drive file to custom location? I am using this code to get the file,

courses.get(0).getCourseMaterialSets().get(0).getMaterials().get(0).getDriveFile()

This function is returning File type output. How to save it locally?

Or is there any way to download google drive files using classroom API?


Solution

  • I don't think there's a way to do this using Classroom API. To download files from Google Drive, check the download files tutorial using Android API for Drive.

    Downloading a file

    Preferred method: using alt=media

    To download files, you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media. For example:

    GET https://www.googleapis.com/drive/v2/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 restricted label to true.

    Here's a snippet from the guide:

    /**
       * Download a file's content.
       *
       * @param service Drive API service instance.
       * @param file Drive File instance.
       * @return InputStream containing the file's content if successful,
       *         {@code null} otherwise.
       */
      private static InputStream downloadFile(Drive service, File file) {
        if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
          try {
            // uses alt=media query parameter to request content
            return service.files().get(file.getId()).executeMediaAsInputStream();
          } catch (IOException e) {
            // An error occurred.
            e.printStackTrace();
            return null;
          }
        } else {
          // The file doesn't have any content stored on Drive.
          return null;
        }
      }