Search code examples
androidgoogle-drive-android-api

Google drive: deprecated method commitAndCloseContents()


I have problem with this method I think commitAndCloseContents is deprecated, but I can't find method to use. Can somebody help?

driveFile.commitAndCloseContents (googleApiClient, result.getDriveContents()).setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(final Status status) {
                if (status.isSuccess()) {
                    Log.d(TAG, "handleUploadOpenContentsResult() commitAndCloseContents() succefully created file " + file.getName());
                    if (onLoadCompleteListener != null)
                        onLoadCompleteListener.onLoadComplete(file);
                } else {
                    Log.w(TAG, "handleUploadOpenContentsResult() commitAndCloseContents() cannot create file " + file.getName());
                    if (onLoadCompleteListener != null)
                        onLoadCompleteListener.onLoadFailed(file, "commitAndCloseContents() failed: " + status.getStatusCode());
                }
                continueUploadFiles();
            }
        });

Solution

  • There is no 'commitAndCloseContents' in the GDAA. There is a commit() method, used anytime you need to access the file contents. Please see this for standard usage of commit() and discard(). In this context it serves as a call to 'close' the content after it was read / written (you can also see the usage here in the read(...) and update(...) primitives).

    Another use for commit() is in the scenario where you need to receive completion events, that tell you when your changes (content, metadata) have been promoted to the Drive. Please see this.

    For completeness, here is a code snippet showing a typical use of commit() / discard().

    GoogleApiClient mGAC;
    ...
    /**************************************************************************
     * update file in GOODrive
     * @param df drive file
     * @param titl  new file name (optional)
     * @param mime  new mime type (optional)
     * @param file  new file content (optional)
     * @return success status
     */
    boolean update(DriveFile df, String titl, String mime, String desc, File file) {
      Boolean bOK = false;
      if (mGAC != null && mGAC.isConnected() && df != null) try {
        Builder mdBd = new Builder();
        if (titl != null) mdBd.setTitle(titl);
        if (mime != null) mdBd.setMimeType(mime);
        if (desc != null) mdBd.setDescription(desc);
        MetadataChangeSet meta = mdBd.build();
    
        MetadataResult r1 = df.updateMetadata(mGAC, meta).await();
        if ((r1 != null) && r1.getStatus().isSuccess() && file != null) {
          DriveContentsResult r2 = df.open(mGAC, DriveFile.MODE_WRITE_ONLY, null).await();
          if (r2.getStatus().isSuccess()) {
            DriveContents cont = file2Cont(r2.getDriveContents(), file);
            Status r3 = cont.commit(mGAC, meta
              //enable if you need COMPLETION EVENTS
              // ,new ExecutionOptions.Builder() .setNotifyOnCompletion(true).build()
            ).await();
            bOK = (r3 != null && r3.isSuccess());
          }
        }
      } catch (Exception e) { e.printStackTrace(); }
      return bOK;
    }
    
    /***********************************************************************
     * get file contents
     * @param df    drive file
     * @return file's content  / null on fail
     */
    InputStream read(DriveFile df) {
      InputStream is = null;
      if (mGAC != null && mGAC.isConnected() && df != null) try {
        DriveContentsResult rslt = df.open(mGAC, DriveFile.MODE_READ_ONLY, null).await();
        if ((rslt != null) && rslt.getStatus().isSuccess()) {
          DriveContents cont = rslt.getDriveContents();
          is = cont.getInputStream();
          cont.discard(mGAC);    // or cont.commit();  they are equiv if READONLY
        }
      } catch (Exception e) { e.printStackTrace(); }
      return is;
    }
    

    Good Luck