Google 驱动器:不推荐使用的方法 commitAndCloseContents()

Google drive: deprecated method commitAndCloseContents()

我对这种方法有疑问,我认为 commitAndCloseContents 已被弃用,但我找不到要使用的方法。有人可以帮忙吗?

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();
            }
        });

read(...)update(...)中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中没有'commitAndCloseContents' 个原语)。

commit() 的另一个用途是在您需要接收完成事件的情况下,它会告诉您您的更改(内容、元数据)何时被提升到驱动器。请参阅 this

为了完整起见,这里有一个代码片段显示了 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;
}

祝你好运