Google云端硬盘:已弃用的方法commitAndCloseContents()

时间:2015-11-25 20:57:57

标签: android google-drive-android-api

我有这个方法的问题我认为不推荐使用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();
            }
        });

1 个答案:

答案 0 :(得分:1)

GDAA中没有'commitAndCloseContents'。有一个 commit()方法,可在您需要访问文件内容时使用。有关提交() discard()的标准用法,请参阅this。在此上下文中,它用作在读取/写入内容后“关闭”内容的调用(您还可以在读取(...)here > update(...) primitives)。

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

祝你好运