Google Drive SDK - 在上传完成时获取资源 ID

GoogleDrive SDK - get resourceId on upload finish

我想在上传后获取 resourceId,但它返回 null。你能帮我得到这个值吗?
提前致谢。

Drive.DriveApi.getFolder(getGoogleApiClient(), driveIdResult.getDriveId())
    .createFile(getGoogleApiClient(), changeSet, driveContents)
    .setResultCallback(new ResultCallbacks<DriveFolder.DriveFileResult>() {
        @Override
        public void onSuccess(@NonNull final DriveFolder.DriveFileResult driveFileResult) {
            if (driveFileResult.getStatus().isSuccess()) {
                String driveId = driveFileResult.getDriveFile().getDriveId().toString();
                //return this:
                //DriveId:CAESHDBCeEc0ZnI0YkQ0ZVZRa1E1YTNVMFVtUTRkRlUY1GcgsPT0w7xTKAA=

                String resourceId = driveFileResult.getDriveFile().getDriveId().getResourceId();
                //null
                //but I want something like this: 0BxG4fr4bD4eVQXRjbTRBV2RMUHc
                uploadCallBack.onFinish(true, resourceId);
            }
        }

        @Override
        public void onFailure(@NonNull com.google.android.gms.common.api.Status status) {
            uploadCallBack.onFinish(false, null);
        }
});

我想你可以听从这位用户的建议:

从您的代码来看,DriveId.toString() returns 一些有用的东西可以用来将其与事件进行比较。我会说:

  • 创建 <String, UploadCallback> 的地图,使用 DriveId.toString 作为键
  • 设置并注册 DriveEventService onCompletion()
  • 在上传结果回调中,调用 file.addChangeSubscription(getGoogleApiClient()) 这样您就可以在 DriveEventService
  • 中收到通知
  • onCompletion() 中,您可以通过调用 event.getDriveId()
  • 获得用于 Map.get() 的 DriveId

我没有测试它,但它应该可以工作,至少在理论上是这样。


编辑: 描述了一种非常相似的方法。记得在构建 ExecutionOptions.

时调用 setNotifyOnCompletion(true)