Android App: Connecting to Google Drive with Google APIs Client Library for Java [Part 3]

posted in: Uncategorized | 0
  1. Android App: Preparing to Access Google Drive
  2. Android App: Setting up Google APIs Client Library for Java
  3. Android App: Connecting to Google Drive with Google APIs Client Library for Java

To get started using Google Drive with Google APIs Client Library for Java you need to connect using OAuth2. To do that requires calling GoogleAccountCredential.usingOAuth2(...) but first you need code similar to the following:

Next in onCreate(...) method you will need to call GoogleAccountCredential.usingOAuth2(...) and then build the drive access as follows:

You need to let the user select an account to which to connect through OAuth2, perhaps in a separate method:

In the onResume() method check if the user has selected an account:

In the onActivityResult(...) handle the result from the startActivityForResult:

In a separate thread, which is needed to prevent the main thread from doing too much work, you can access Google Drive to create a folder, upload, and perform other Drive operations:

To create a folder inside another folder (subfolder):

The method to upload a file is straight from this post on Stack Overflow titled How to show uploading to Google Drive progress in my android App?:

Available mime types that are supported by Google Drive can be found in the following links:

For more operations that can done using Google Drive:

And there you have it. A brief introduction on how to setup and use Google Drive.