2017-04-11 50 views
0

我试图使用Google Drive API来允许用户选择一个文件,并且(到目前为止)获取它的元数据。Android:Google Drive getMetaData()不调用回调

下面是我在哪里(提取):

@Override 
public void onConnected(Bundle connectionHint) { 
    super.onConnected(connectionHint); 
    IntentSender intentSender = Drive.DriveApi 
     .newOpenFileActivityBuilder() 
     .build(getGoogleApiClient()); 
    try { 
     startIntentSenderForResult(
      intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0); 
    } catch (SendIntentException e) { 
     Log.w(TAG, "Unable to send intent", e); 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case REQUEST_CODE_OPENER: 
      if (resultCode == RESULT_OK) { 
       if (data != null) { 
        DriveId driveId = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID); 
        Log.d(TAG, "driveId = " + driveId.encodeToString()); 
        //driveId.asDriveResource().getMetadata(getGoogleApiClient()) 
        driveId.asDriveFile().getMetadata(getGoogleApiClient()) 
         .setResultCallback(new ResultCallback<DriveResource.MetadataResult>() { 
          public void onResult(@NonNull DriveResource.MetadataResult mdr) { 
           // Never gets here 
          } 
         }); 
       } 
      } 
      break; 
     default: 
      super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

似乎工作得很好。它记录它找到的DriveId,并获取到getMetadata()呼叫。但它永远不会调用回调:onResult()永远不会到达。

我觉得我几乎肯定会做出明显错误的事情,但在研究API文档和Google示例代码时,我无法发现它是什么。

编辑补充:

我试图用setResultCallback()await()(在一个线程)改变它,但同时DriveID再次精细,MetadataResultCANCELED一个statusCode(即使我按SELECT从Drive文件选择器)。所以打电话给getMetadata()返回null

这是否表明事情可能出错?

回答

0

检查的getMetaData()。setResultCallBack()代码here

在上面的代码中,你却把onResult块空。 您应该使用onResult块中的MetaDataResult obj来获取有关所需文件/文件夹的元数据。

检查状态,如果在MetaDataResult OBJ

final ResultCallback<MetadataResult> metadataCallback = new ResultCallback<MetadataResult>() { 
    @Override 
    public void onResult(MetadataResult result) { 
     if (!result.getStatus().isSuccess()) { 
      showMessage("Problem while trying to retrieve the file metadata"); 
      return; 
     } 
     if (result.getMetadata().isPinnable()) { 
      showMessage("File is pinnable"); 
     } 
}; 

成功与否更多元数据看android doc


其驱动器Id那不是匹配的,如果它的元数据的回调并不成功

尝试在此SO线程中获取该文件的正确ID的方法link

u能做到这一点在驱动器Id u得到:

Drive.DriveApi.fetchDriveId(getGoogleApiClient(), driveId.getResourceId()).setResultCallback(idCallback); 

final ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() { 
    @Override 
    public void onResult(DriveApi.DriveIdResult result) { 
     if (!result.getStatus().isSuccess()) { 
      showMessage("Cannot find DriveId. Are you authorized to view this file?"); 
      return; 
     } 
     DriveId driveId = result.getDriveId(); 
     showLogDebug(driveId + " in idCallback of fetchId"); 
     DriveFile file = driveId.asDriveFile(); 
     file.getMetadata(getGoogleApiClient()).setResultCallback(metadatacallBack); 

} };

+0

谢谢。对不起,我没有更清楚:为了简化示例,我删除了实际的'onResult'代码,并将其替换为该注释,因为执行从未到达任何地方。在我给出的例子中,如果我将代码放在那里,它永远不会检查'isSuccess()'。 –