title: Sync SQLite Database to google drive without confirmation
body: <p>I have to implement synchronization  in my android app. So, I need to use user's google drive to store and retrieve info. I have created functionality to store *.db file to drive. But problem is user has to confirm file upload operation every time. I want to upload file on every app close without confirmation. Is it possible ? Is there other way to synchronize sqlite db without having own server ?</p>
  
  <pre><code>public static void backUpDb(final Activity context){
      mGoogleApiClient = new GoogleApiClient.Builder(context)
      .addApi(Drive.API)
      .addScope(Drive.SCOPE_FILE)
      .addConnectionCallbacks(new ConnectionCallbacks() {
          @Override
          public void onConnectionSuspended(int arg0) {
          }
  
          @Override
          public void onConnected(Bundle arg0) {
  
              Log.i(TAG, "API client connected.");
              saveFileToDrive( context);
          }
      })
      .addOnConnectionFailedListener(
              new OnConnectionFailedListener() {
  
                  @Override
                  public void onConnectionFailed(
                          ConnectionResult result) {
                      Log.i(TAG,
                              "GoogleApiClient connection failed: "
                                      + result.toString());
                      if (!result.hasResolution()) {
                          // show the localized error
                          // dialog.
                          GooglePlayServicesUtil
                                  .getErrorDialog(
                                          result.getErrorCode(),
                                          context, 0).show();
                          return;
                      }
  
                      try {
                          result.startResolutionForResult(
                                  context,
                                  REQUEST_CODE_RESOLUTION);
                      } catch (SendIntentException e) {
                          Log.e(TAG,
                                  "Exception while starting resolution activity",
                                  e);
                      }
  
                  }
              }).build();
      mGoogleApiClient.connect();
  
  
      //mGoogleApiClient.disconnect();        
  }
  
  
  
  public static void saveFileToDrive(final Activity context) {
      Drive.DriveApi.newContents(mGoogleApiClient).setResultCallback(
              new ResultCallback&lt;ContentsResult&gt;() {
  
                  @Override
                  public void onResult(ContentsResult result) {
                      if (!result.getStatus().isSuccess()) {
  
                          return;
                      }
                      OutputStream outputStream = result.getContents()
                              .getOutputStream();
                      try {
                          // outputStream.write(bitmapStream.toByteArray());
  
                          //ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                          File dbFile = context.getDatabasePath(databaseName);
                          //dbFile.
                          int size = (int) dbFile.length();
                          byte[] bytes = new byte[size];
                          BufferedInputStream buf = new BufferedInputStream(new FileInputStream(dbFile));
                          buf.read(bytes, 0, bytes.length);
                          buf.close();
  
                          byte[] b = "aa".getBytes();
                          outputStream.write(bytes);
                      } catch (IOException e1) {
                          Log.i(TAG, "Unable to write file contents.");
                      }
  
                      String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");
                      MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                              .setMimeType(mimeType).setTitle("aldkan.db")
                              .build();
                      IntentSender intentSender = Drive.DriveApi
                              .newCreateFileActivityBuilder()
                              .setInitialMetadata(metadataChangeSet)
                              .setInitialContents(result.getContents())
                              .build(mGoogleApiClient);
                      try {
                          context.startIntentSenderForResult(intentSender, 2,
                                  null, 0, 0, 0);
                      } catch (SendIntentException e) {
  
                          e.printStackTrace();
                      }
                  }
              });
  }
  </code></pre>