Previously, I'm using "API Client Library for Java" to perform file search in appdata folder.
File searching code in API Client Library for Java
private static void searchFromGoogleDrive(Drive drive, String qString) {
try {
Files.List request = drive.files().list().setQ(qString);
do {
FileList fileList = request.execute();
for (com.google.api.services.drive.model.File f : fileList.getItems()) {
final String title = f.getTitle();
if (title == null || f.getDownloadUrl() == null || f.getDownloadUrl().length() <= 0) {
continue;
}
...
}
request.setPageToken(fileList.getNextPageToken());
} while (request.getPageToken() != null && request.getPageToken().length() > 0);
} catch (IOException ex) {
log.error(null, ex);
return;
}
return;
}
It works perfectly fine for the following query string.
title contains 'jstock-fe78440e-e0fe-4efb' and trashed = false and 'appdata' in parents and 'me' in owners
Recently, I try to migrate the code to use "Drive API for Android". I have 2 scopes for GoogleApiClient
- Drive.SCOPE_FILE
- Drive.SCOPE_APPFOLDER
File searching code in Drive API for Android
private static void searchFromGoogleDrive(GoogleApiClient googleApiClient) {
DriveFolder driveFolder = Drive.DriveApi.getAppFolder(googleApiClient);
Query query = new Query.Builder()
.addFilter(Filters.and(
Filters.contains(SearchableField.TITLE, "jstock-fe78440e-e0fe-4efb"),
Filters.eq(SearchableField.TRASHED, false),
Filters.ownedByMe()
))
.build();
DriveApi.MetadataBufferResult metadataBufferResult = driveFolder.queryChildren(googleApiClient, query).await();
if (metadataBufferResult == null) {
Log.i("CHEOK", "metadataBufferResult is null");
} else {
Log.i("CHEOK", "metadataBufferResult's status = " + metadataBufferResult.getStatus().isSuccess());
}
if (metadataBufferResult != null && metadataBufferResult.getStatus().isSuccess()) {
MetadataBuffer metadataBuffer = null;
try {
metadataBuffer = metadataBufferResult.getMetadataBuffer();
if (metadataBuffer != null ) {
for (Metadata md : metadataBuffer) {
if (md == null || !md.isDataValid()) {
continue;
}
Log.i("CHEOK", "title = " + md.getTitle());
} // for
} // if
} finally {
if (metadataBuffer != null) {
metadataBuffer.release();
}
}
}
return;
}
I realize, Filters.ownedByMe
is the one which causes the problem.
Here's the different outcomes based on different Queries.
Failed
Query query = new Query.Builder()
.addFilter(Filters.and(
Filters.contains(SearchableField.TITLE, "jstock-fe78440e-e0fe-4efb"),
Filters.eq(SearchableField.TRASHED, false),
Filters.ownedByMe()
))
.build();
// metadataBufferResult's status = false
Failed
Query query = new Query.Builder()
.addFilter(Filters.and(
Filters.ownedByMe()
))
.build();
// metadataBufferResult's status = false
Success
Query query = new Query.Builder()
.addFilter(Filters.and(
Filters.contains(SearchableField.TITLE, "jstock-fe78440e-e0fe-4efb"),
Filters.eq(SearchableField.TRASHED, false)
))
.build();
// metadataBufferResult's status = true
// title = jstock-fe78440e-e0fe-4efb-881d-264a01be483c-checksum=2298926779-date=1452429908167-version=1107.zip
It seems that Filters.ownedByMe()
doesn't work as correctly in Drive API for Android
Is there anything I can rectify this?
来源:https://stackoverflow.com/questions/34705929/filters-ownedbyme-doesnt-work-in-drive-api-for-android-but-works-correctly-i