title: Android Update the file content of created file on Google Drive
body: <p>I have done Google drive integration and in integration i have created a file and write some text on the file and save it into the Login user's drive it is running fine but i want to to update the contents of the file i have created a save to drive, i have done allot of research but did not able to find the demo or any code, please any one guide me, i will post my code where i am creating file and saving to my drive</p>
  
  <pre><code> public class MainActivity extends Activity implements ConnectionCallbacks,OnConnectionFailedListener {
  
      private GoogleApiClient mGoogleApiClient;
      Button b ,editfile;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Drive.API)
                  .addScope(Drive.SCOPE_FILE)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .build();
  
          b = (Button) findViewById(R.id.createfile);
          editfile = (Button) findViewById(R.id.editfile);
          b.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
  
                  // create new contents resource
                  /**
                   * A code to illustrate how to create a new FILE in the Drive.
                   */
                  Drive.DriveApi.newContents(getGoogleApiClient())
                          .setResultCallback(contentsCallback);
  
              }
          });
  
  
  
      }
  
  
      @Override
      protected void onStart() {
          super.onStart();
          mGoogleApiClient.connect();
      }
  
       /**
       * Called when activity gets visible. A connection to Drive services need to
       * be initiated as soon as the activity is visible. Registers
       * {@code ConnectionCallbacks} and {@code OnConnectionFailedListener} on the
       * activities itself.
       */
      @Override
      protected void onResume() {
          super.onResume();
          if (mGoogleApiClient == null) {
              mGoogleApiClient = new GoogleApiClient.Builder(this)
                      .addApi(Drive.API)
                      .addScope(Drive.SCOPE_FILE)
                      //.addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                      .addConnectionCallbacks(this)
                      .addOnConnectionFailedListener(this)
                      .build();
          }
          mGoogleApiClient.connect();
      }
      @Override
      public void onConnected(Bundle arg0) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onConnectionSuspended(int arg0) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
          if (connectionResult.hasResolution()) {
              try {
                  connectionResult.startResolutionForResult(this, 1);
              } catch (IntentSender.SendIntentException e) {
                  // Unable to resolve, message user appropriately
              }
          } else {
              GooglePlayServicesUtil.getErrorDialog(
                      connectionResult.getErrorCode(), this, 0).show();
          }
      }
  
      @Override
      protected void onActivityResult(final int requestCode,
              final int resultCode, final Intent data) {
          switch (requestCode) {
          case 1:
              if (resultCode == RESULT_OK) {
                  mGoogleApiClient.connect();
              }
              break;
          }
      }
  
      public GoogleApiClient getGoogleApiClient() {
          return mGoogleApiClient;
      }
  
  
  
  
  
      /**
       * A code to illustrate how to create a new FILE with some text on the file in the Google Drive.
       */
      final private ResultCallback&lt;ContentsResult&gt; contentsCallback = new ResultCallback&lt;ContentsResult&gt;() {
          @Override
          public void onResult(ContentsResult result) {
              if (!result.getStatus().isSuccess()) {
                  Toast.makeText(MainActivity.this,"Error while trying to create new file contents" ,Toast.LENGTH_LONG).show();
                  return;
              }
  
              Contents driveContents = result.getContents();
              // write content to DriveContents
              OutputStream outputStream = driveContents.getOutputStream();
              Writer writer = new OutputStreamWriter(outputStream);
              try {
                  writer.write(information_data());
                  writer.close();
              } catch (IOException e) {
                  Log.e("IOExceptions=", e.toString());
              }
  
              MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                      .setTitle("testFile.txt")
                      .setMimeType("text/plain")
                      .setStarred(true)
                      .build();
              // create a file on root folder
              Drive.DriveApi.getRootFolder(getGoogleApiClient())
                      .createFile(getGoogleApiClient(), changeSet, result.getContents())
                      .setResultCallback(fileCallback);
          }
      };
  
      final private ResultCallback&lt;DriveFileResult&gt; fileCallback = new ResultCallback&lt;DriveFileResult&gt;() {
          @Override
          public void onResult(DriveFileResult result) {
              if (!result.getStatus().isSuccess()) {
                  Toast.makeText(MainActivity.this,"Error while trying to create the file" ,Toast.LENGTH_LONG).show();
  
                  return;
              }
              Toast.makeText(MainActivity.this,"Created a file: " + result.getDriveFile().getDriveId(),Toast.LENGTH_LONG).show();
              Log.e("result.getDriveFile().getDriveId=", ""+result.getDriveFile().getDriveId());
              Log.e("result.getStatus()=", ""+result.getStatus().toString());
  
              //Opening the file contents Reading files
              driveOpnefileContents(result.getDriveFile());
  
          }
      };
  
      public  void driveOpnefileContents(DriveFile driveFile) {
          // TODO Auto-generated method stub
          Log.e("driveOpnefileContents=", "driveOpnefileContents");
          driveFile.openContents(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, new DownloadProgressListener() {
  
              @Override
              public void onProgress(long bytesDownloaded, long bytesExpected) {
                   // Update progress dialog with the latest progress.
                  Log.e("onProgress=", "onProgress");
                  int progress = (int)(bytesDownloaded*100/bytesExpected);
                  Log.e("progress", String.format("Loading progress: %d percent=", progress));
              }
          })
          .setResultCallback(contentsOpenedCallback);
  
      }
  
      ResultCallback&lt;ContentsResult&gt; contentsOpenedCallback = new ResultCallback&lt;ContentsResult&gt;() {
          @Override
          public void onResult(ContentsResult result) {
              if (!result.getStatus().isSuccess()) {
                  // display an error saying file can't be opened
                   Toast.makeText(MainActivity.this,"display an error saying file can't be opened" ,Toast.LENGTH_LONG).show();
                  return;
              }
              // DriveContents object contains pointers
              // to the actual byte stream
              try {
                  Contents driveContents = result.getContents();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(driveContents.getInputStream()));
                  StringBuilder builder = new StringBuilder();
                  String line;
                  while ((line = reader.readLine()) != null) {
                      builder.append(line);
                  }
                  String contentsAsString = builder.toString();
                  Log.e("Reading File Contents As String=", ""+contentsAsString);
                  driveContents.close();
                  /*try {
                      ParcelFileDescriptor parcelFileDescriptor = driveContents.getParcelFileDescriptor();
                      FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
                      // Read to the end of the file.
                      fileInputStream.read(new byte[fileInputStream.available()]);
  
                      // Append to the file.
                      FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
                      Writer writer = new OutputStreamWriter(fileOutputStream);
                      writer.write("editing the contents of the saved file");
                      writer.close();
  
                      driveContents.close();
  
                  } catch (IOException e) {
                      Log.e("IOExceptionAppend to the file.=", e.toString());
                      //java.io.IOException: write failed: EBADF (Bad file number)
                  }*/
                  Log.e("Append to the file.=", "Append to the file.");
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  Log.e("IOExceptionAppend to the file2.=", e.toString());
              }
  
          }
      };
  
      public String information_data(){
          String result = "";
          try {
              JSONObject jArrayFacebookData = new JSONObject();
              JSONObject jObjectType = new JSONObject();
              // put elements into the object as a key-value pair
              jObjectType.put("info", "facebook_login");
              jArrayFacebookData.put("Result", jObjectType);
              // 2nd array for user information
              JSONObject jObjectData = new JSONObject();
              // Create Json Object using Facebook Data
              jObjectData.put("facebook_user_id", "facebook_user_id");
              jObjectData.put("first_name", "achin");
              jObjectData.put("last_name", "verma");
              jObjectData.put("email", "xvz");
              jObjectData.put("username", "achin");
              jObjectData.put("birthday", "28 april 90");
              jObjectData.put("gender", "male");
              jObjectData.put("location", "mohali");
              jObjectData.put("display_photo", "link");
              jArrayFacebookData.put("data", jObjectData);
              //Log.e("jArrayFacebookData=", ""+jArrayFacebookData);
              result = ""+jArrayFacebookData;
          } catch (JSONException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
          return result;
  
      }
  
  }
  </code></pre>