我使用Google Drive API查询文件并检索其元数据。当在之前从未安装的设备上运行应用程序时,返回的MetadataBuffer为空(正如我从logcat中看到的那样)。 如果我卸载应用程序并将其重新安装回,则相同的代码将生成一个带有预期的元数据对象的MetaDataBuffer。这发生在我测试的每个设备上。我认为它与本地缓存有关。我想知道我是否在我的代码中丢失了某些东西,或者我最终应该向Google提交问题。
下面是完整的代码:Google云端硬盘查询不检索文件 - 仅第一次
public void setupClient(boolean noProgress) {
Log.i("drive", "setup client...");
mGoogleApiClient = new GoogleApiClient.Builder(parent)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(connectionCallbacks)
.addOnConnectionFailedListener(connectionFailedListener)
.build();
mGoogleApiClient.connect();
}
private GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i("drive", "connected");
Filter mime = Filters.eq(SearchableField.MIME_TYPE, MIME_TYPE);
Filter trashed = Filters.eq(SearchableField.TRASHED, false);
Query query = new Query.Builder()
.addFilter(mime)
.addFilter(trashed)
.build();
Drive.DriveApi.query(mGoogleApiClient,query).setResultCallback(metadataCallback);
}
private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback = new
ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
Log.i("drive", String.valueOf(buffer.getCount()));
if (buffer.getCount()>0) {
/* this is where it fails:
the first time, getCount() is 0. the following times,
getCount() is the right number of files in the Drive */
Log.i("drive","files found");
//do things with metadata...
} else {
Log.i("drive","files not found");
//do other things when there are no files...
}
}
};
我有同样的问题,但我可以每次安装应用程序时重现它 – Simon