Google Drive API
Google Drive is a file hosting service created by Google. It provides file storage service and allows the user to upload files in the cloud and also share with other people. Using Google Drive API, we can synchronize files between computer or mobile device and Google Drive Cloud.
If you use the Google Drive Android API in your application, you must include the Google Play Services attribution text as part of a "Legal Notices" section in your application.
It’s recommended that you include legal notices as an independent menu item, or as part of an "About" menu item.
You can make a call to
GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo() to get the attribution text at runtime.
Create a File on Google Drive
We will add a file on Google Drive. We will use the
createFile() method of a
Drive object to create file programmatically on Google Drive. In this example we are adding a new text file in the user’s root folder. When a file is added, we need to specify the initial set of metadata, file contents, and the parent folder.
We need to create a
CreateMyFile() callback method and within this method, use the
Drive object to retrieve a
DriveContents resource. Then we pass the API client to the
Drive object and call the
driveContentsCallback callback method to handle result of
DriveContents resource contains a temporary copy of the file's binary stream which is only available to the application.
Result Handler of DriveContents
Handling the response requires to check if the call was successful or not. If the call was successful, we can retrieve the
We will create a result handler of
DriveContents. Within this method, we call the
CreateFileOnGoogleDrive() method and pass the result of
Create File Programmatically
To create files, we need to use a
MetadataChangeSet object. By using this object, we set the title (file name) and file type. Also, we must use the
createFile() method of the
DriveFolder class and pass the Google client API, the
MetaDataChangeSet object, and the
driveContents to create a file. We call the result handler callback to handle the result of the created file.
We use the following code to create a new text file in the user's root folder:
Handle result of Created File
The following code will create a callback method to handle the result of the created file:
Integrate Google Drive in Android
Create a New Project on Google Developer Console
To integrate Android application with Google Drive, create the credentials of project in the Google Developers Console. So, we need to create a project on Google Developer console.
To create a project on Google Developer Console, follow these steps:
Go to Google Developer Console for Android. Fill your project name in the input field and click on the create button to create a new project on Google Developer console.
We need to generate SHA-1 fingerprint. So, open your terminal and run Keytool utility to get the SHA1 fingerprint. While running Keytool utility, you need to provide keystore password. Default development keytool password is “android”.
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v
Enable Google Drive API
We need to enable Google Drive Api to access files stored on Google Drive from Android application. To enable Google Drive API, follow below steps:
Go to your Google Developer console Dashboard and click on Enable APIs get credentials like keys then you will see popular Google APIs.
Add Internet Permission
App needs Internet access Google Drive files. Use the following code to set up Internet permissions in AndroidManifest.xml file :
Add Google Play Services
We will use Google play services API which includes the Google Drive Android API. So, we need to setup Google play services SDK in Android Application. Open your
build.gradle(app module) file and add Google play services SDK as a dependencies.
Add API key in Manifest file
To use Google API in Android application, we need to add API key and version of the Google Play Service in the AndroidManifest.xml file. Add the correct metadata tags inside the tag of the AndroidManifest.xml file.
Connect and Authorize the Google Drive Android API
We need to authenticate and connect Google Drive Android API with Android application. Authorization of Google Drive Android API is handled by the GoogleApiClient. We will use GoogleApiClient within onResume() method.
Disconnect Google Deive Android API
When activity stops, we will disconnected Google Drive Android API connection with Android application by calling disconnect() method inside activity’s onStop() method.
Implement Connection Callbacks and Connection Failed Listener
We will implement Connection Callbacks and Connection Failed Listener of Google API client in MainActivity.java file to know status about connection of Google API client. These listeners provide onConnected(), onConnectionFailed(), onConnectionSuspended() method to handle the connection issues between app and Drive.
If user has authorized the application, the onConnected() method is invoked. If user has not authorized application, onConnectionFailed() method is invoked and a dialog is displayed to user that your app is not authorized to access Google Drive. In case connection is suspended, onConnectionSuspended() method is called.
You need to implement ConnectionCallbacks and OnConnectionFailedListener in your activity. Use the following code in your Java file.