Android™ Google Drive: Download a File (Stream to the Filesystem)

Back to Index

Downloads the content of a file by ID. (In this example the file id = 1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl) The file is streamed directly to the filesystem. Note: The alt=media query param must be used to download the file content (as opposed to the file metadata).

Documentation: https://developers.google.com/drive/v3/reference/files/get


// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CkRest rest = new CkRest();
    boolean success;

    //   Provide a previously obtained OAuth2 access token.
    CkOAuth2 oauth2 = new CkOAuth2();
    oauth2.put_AccessToken("OAUTH2_ACCESS_TOKEN");
    rest.SetAuthOAuth2(oauth2);

    success = rest.Connect("www.googleapis.com",443,true,true);
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    rest.AddQueryParam("includeTeamDriveItems","true");
    rest.AddQueryParam("supportsTeamDrives","true");
    rest.AddQueryParam("alt","media");

    //  First send the HTTP request.
    success = rest.SendReqNoBody("GET","/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl");
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    //  Read the response header.  If the response status code is success, stream to the file.
    //  Otherwise receive the error response text.
    int statusCode = rest.ReadResponseHeader();
    if (statusCode < 0) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    if (statusCode != 200) {
        Log.i(TAG, "Received error response code: " + String.valueOf(statusCode));
        //  Read the error response body.
        CkStringBuilder sbErrResponse = new CkStringBuilder();
        success = rest.ReadRespSb(sbErrResponse);
        if (success != true) {
            Log.i(TAG, rest.lastErrorText());
            return;
            }

        Log.i(TAG, "Error response:" + sbErrResponse.getAsString());
        return;
        }

    //  Stream the response body to the output file.
    CkStream respBodyStream = new CkStream();
    respBodyStream.put_SinkFile("/someDirectory/penguins.jpg");
    success = rest.ReadRespBodyStream(respBodyStream,true);
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    Log.i(TAG, "Example Completed.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}