When testing of delete, trash functionality discussed in SO 22295903, I've run into this issue.
1/ Create a file with contents
GoogleApiClient _gac;
DriveFile createFileWait(DriveFolder fldr, String name, String mime, byte[] buff) {
DriveFile drvFile = null;
try {
ContentsResult rslt = Drive.DriveApi.newContents(_gac).await();
if (rslt.getStatus().isSuccess()) {
Contents cont = rslt.getContents();
cont.getOutputStream().write(buff);
MetadataChangeSet meta = (mime == null) ?
new MetadataChangeSet.Builder().setTitle(name).build() :
new MetadataChangeSet.Builder().setTitle(name).setMimeType(mime).build();
drvFile = fldr.createFile(_gac, meta, cont).await().getDriveFile();
}
} catch (Exception e) {}
return drvFile;
}
2/ retrieve the file using query (it's title):
ArrayList<DriveId> findAll(String title, String mime, DriveFolder fldr) {
ArrayList<DriveId> dIDs = null;
if (isConnected()) try {
ArrayList<Filter> fltrs = new ArrayList<Filter>();
fltrs.add(Filters.eq(SearchableField.TRASHED, false));
if (title != null) fltrs.add(Filters.eq(SearchableField.TITLE, title));
if (mime != null) fltrs.add(Filters.eq(SearchableField.MIME_TYPE, mime));
Query qry = new Query.Builder().addFilter(Filters.and(fltrs)).build();
MetadataBufferResult rslt = (fldr == null) ? Drive.DriveApi.query(_gac, qry).await() :
fldr.queryChildren(_gac, qry).await();
if (rslt.getStatus().isSuccess()) {
MetadataBuffer mdb = null;
try {
mdb = rslt.getMetadataBuffer();
if (mdb == null) return null;
dIDs = new ArrayList<DriveId>();
for (Metadata md : mdb) {
if ((md == null) || md.isTrashed()) continue;
dIDs.add(md.getDriveId());
}
} finally { if (mdb != null) mdb.close(); }
}
} catch (Exception e) {}
return dIDs;
}
3/ You get valid DriveId. Try to use it to gain resource ID to use in RESTful API, or elsewhere.
String fileID = drvId.getResourceId();
You get a null value. After a few moments (random, difficult to specify), if you repeat the query, you'll finally get your resource ID. I know why, it is probably a latency issue. I'm just soliciting a comment from the Google Support Team. Is there a way to gain control? Query latency status?
This happens because changes are persisted locally first, and then uploaded to the server at a (possibly) later time when we have sufficient network connectivity. Unfortunately the resource id is not available until the newly created file is committed to the server.
Currently all you can do is wait for it to be available. We are working on some additions that will make this flow easier, so stay tuned.
来源:https://stackoverflow.com/questions/22432431/google-drive-android-api-gdaa-getresourceid-returning-null-timing-issue