1 – Create a New Project on Google Developer Console
2 – Enable Google Drive API
Then :
build.gradle
compile 'com.google.android.gms:play-services-drive:10.2.1'
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ir.mhdr.a068"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" /> <meta-data android:name="com.google.android.gms.appstate.APP_ID" android:value="@string/app_id" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> </application> </manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private static final int REQUEST_CODE_CREATOR = 1; private static final int REQUEST_CODE_RESOLUTION = 2; private GoogleApiClient mGoogleApiClient; Button buttonSave; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonSave= (Button) findViewById(R.id.buttonSave); buttonSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveFileToDrive(); } }); } @Override protected void onResume() { super.onResume(); if (mGoogleApiClient == null) { // Create the API client and bind it to an instance variable. // We use this instance as the callback for connection and connection // failures. // Since no account name is passed, the user is prompted to choose. mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } mGoogleApiClient.connect(); } @Override protected void onPause() { if (mGoogleApiClient != null) { mGoogleApiClient.disconnect(); } super.onPause(); } @Override public void onConnected(@Nullable Bundle bundle) { Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show(); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult result) { // Called whenever the API client fails to connect. if (!result.hasResolution()) { // show the localized error dialog. GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show(); return; } // The failure has a resolution. Resolve it. // Called typically when the app is not yet authorized, and an // authorization // dialog is displayed to the user. try { result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } /** * Create a new file and save it to Drive. */ private void saveFileToDrive() { // Start by creating a new contents, and setting a callback. Drive.DriveApi.newDriveContents(mGoogleApiClient) .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { @Override public void onResult(DriveApi.DriveContentsResult result) { // If the operation was not successful, we cannot do anything and must fail if (!result.getStatus().isSuccess()) { return; } // Otherwise, we can write our data to the new contents. // Get an output stream for the contents. OutputStream outputStream = result.getDriveContents().getOutputStream(); Writer writer=new OutputStreamWriter(outputStream); try { writer.write("Hello World"); writer.flush(); writer.close(); } catch (IOException e1) { e1.printStackTrace(); } // Create the initial metadata - MIME type and title. // Note that the user will be able to change the title later. MetadataChangeSet metadataChangeSet = new MetadataChangeSet .Builder() .setMimeType("text/plain") .setTitle("Hello.txt") .setStarred(true) .build(); // Create an intent for the file chooser, and start it. IntentSender intentSender = Drive.DriveApi .newCreateFileActivityBuilder() .setInitialMetadata(metadataChangeSet) .setInitialDriveContents(result.getDriveContents()) .build(mGoogleApiClient); try { startIntentSenderForResult( intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } }); } }
References
https://github.com/mhdr/AndroidSamples/tree/master/068