This API is deprecated. Clients must migrate to the Drive REST API or another suitable solution to avoid disruptions to your application.

Using the Google APIS Client Library for Java to integrate with Drive on Android

In some cases, your application may need to use the Google APIs Client Library for Java to access features or authorization scopes that are not available in the Google Drive Android API.

Using the Google Drive web services through the Java client library and the Android Development Kit, you can integrate a mobile app with Google Drive on Android. When Android Drive users choose to open files with MIME types supported by your mobile app, they can select your app from a list and open the file.

Open with dialog for selecting a Drive mobile app

By integrating with the Android Drive App, you're able to rely on the recommended per-file Drive scope for access to user's files. This is the most secure and efficient way to let your mobile app list or access the files in a user's Drive.

Authorize your app on Android

To authorize access, follow the instructions in Connecting and Authorizing with the Google API Java Client. For additional information on authorization with Android, see "Google Play Services authorization mechanism" in the Android documentation.

When authorizing, be sure you use the following Drive scope:

https://www.googleapis.com/auth/drive.file

This scope allows per-file access to files created or opened by the app. For more information about Drive scopes, see Choose Auth Scopes.

Set MIME types in the app manifest

To make sure your app is included in the list of apps that can open a file, you need to make some additions to your app manifest. The app manifest must specify an exported activity for opening Drive files, and this activity must specify the MIME types that your app can open.

The following example shows the manifest for hypothetical image editing app called "CloudPaint" with the ID 1234567890. Your own app manifest must include a similar intent with your own appropriate values and MIME types that your app can open and create:

<manifest …>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <application …>
        <activity android:name="CloudPaintActivity" android:label="@string/cloud_paint"
          android:icon="@drawable/app_icon" android:exported="true" >
        <meta-data android:name="com.google.android.apps.drive.APP_ID" android:value="id=1234567890" />
        <intent-filter>
        <action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
        <data android:mimeType="application/vnd.google-apps.drive-sdk.1234567890" />
        <data android:mimeType="image/png" />
        <data android:mimeType="image/jpeg" />
        <data android:mimeType="image/jpg" />
        </intent-filter>
    </activity>
</application>
</manifest>

The important aspects of this manifest to note are:

  • The uses-permission tag, which lets the user grant the app permission to user account information if the app needs it.
  • The App ID, as registered in your Google API project.
  • The action com.google.android.apps.drive.DRIVE_OPEN.
  • The MIME type application/vnd.google-apps.drive-sdk.YOUR_APP_ID. This is an optional type that allows the app to open shortcuts.

For more information about the Android app manifest, see The AndroidManifest.xml File.

Handle the intent in your app

When a user selects your app to open a file, the Drive UI sends an intent to your app containing an action, data and type. The intent filter specified in the manifest only allows apps that have all three of these fields set:

  • action — the open action, com.google.android.apps.drive.DRIVE_OPEN
  • data — the file URI, which contains the file ID. For example, content://com.google.android.drive/open/FILE_ID
  • type — the MIME type of the file retrieved

The Activity handing the intent can retrieve the file ID as shown in this example:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * @author alainv
 *
 */
public class MyActivity extends Activity {

 /** DRIVE_OPEN Intent action. */
 private static final String ACTION_DRIVE_OPEN = "com.google.android.apps.drive.DRIVE_OPEN";
 /** Drive file ID key. */
 private static final String EXTRA_FILE_ID = "resourceId";

 /** Drive file ID. */
 private String mFileId;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   // Get the action that triggered the intent filter for this Activity
   final Intent intent = getIntent();
   final String action = intent.getAction();

   // Make sure the Action is DRIVE_OPEN.
   if (ACTION_DRIVE_OPEN.equals(action)) {
     // Get the Drive file ID.
     mFileId = intent.getStringExtra(EXTRA_FILE_ID);
     getUserAccountAndProcessFile();
   } else {
     // Unknown action.
     finish();
   }
 }

 /**
  * Prompt the user to choose the account to use and process the file using the
  * Drive file ID stored in mFileId.
  */
 private void getUserAccountAndProcessFile() {
   // Implement the method.
   throw new UnsupportedOperationException(
       "The getUserAccountAndProcessFile method has not been implemented");
 }

}

Once it has the file ID, an app can check permissions, fetch the file metadata, and download the file content as described in the reference documentation for files.get. When your app modifies files, use files.update to push your changes to Drive.

Send feedback about...

Drive API for Android (Deprecated)
Drive API for Android (Deprecated)
Need help? Visit our support page.