I am developing an application for Android, where I need to get the list of parents of a file taken from google Drive.
I correctly obtain the DriveId using Drive.DriveApi.newOpenFileActivityBuilder(), but when I use DriveResource.listParents I obtain an empty list even if the resource have a parent.
I use the same GoogleApiClient for Drive.DriveApi.newOpenFileActivityBuilder() and DriveResource.listParents, so i do not think that is a scope problem.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_CODE_GOOGLE_DRIVE_FILE_PICKER) {
if (resultCode == RESULT_OK) {
mSelectedFileDriveId = (DriveId) data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
} else {
setResult(Constants.RESULT_CODE_KO);
finish();
}
}
}
...
private ResultCallback<MetadataBufferResult> metadataBufferCallback = new ResultCallback<MetadataBufferResult>() {
@Override
public void onResult(MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
SDKUtils.showMessage(thisActivity, "Impossible salvare l'ultima posizione aperta in Google Drive");
return;
}
MetadataBuffer metadataBuffer = result.getMetadataBuffer();
// HERE I OBTAIN AN EMPTY OBJECT
}
};
...
mGoogleApiClient = new GoogleApiClient.Builder(this)
.setAccountName(driveAccontName)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
...
DriveFile driveFile = Drive.DriveApi.getFile(getGoogleApiClient(), mSelectedFileDriveId);
driveFile.listParents(GoogleApiClient apiClient).setResultCallback(metadataBufferCallback);
Have any suggestions? Thanks!
so i do not think that is a scope problem.
It's exactly that - Scope problem !!!
Google Drive Android API provides only drive.file and drive.appfolder scope.
File scope means access only to those files which the user chooses from your application. The
Drive.DriveApi.newOpenFileActivityBuilder()
dialog will let the user browse through the whole drive but your application will be granted access to only those file or folder which the user specificallySelect
's.Suppose you have following folder structure on your drive -
Now using Drive.DriveApi.newOpenFileActivityBuilder() you pick up programming_gems.pdf. Your app has now obtained access to that pdf. You can retrieve the file and its metadata. But your app won't be able to obtain access to its parent folder, c++ in this case. Why? because the user has not yet given access to c++ folder; only a specific file within that folder. This makes sense also; otherwise you can retrieve any file from that folder leaving your user worried.
Pick up c++ folder using the same Drive.DriveApi.newOpenFileActivityBuilder(). Now if your app tries to retrieve the parent folder of programming_gems.pdf, it is granted access.
Try one more thing now; try to list the contents of c++ folder itself. What wil be returned to your app? - only programming_gems.pdf. Why, because the user has not granted access to the other file, c++_notes.docs.
I hope am clear.
P.S.
By the way, to select between picking up files or folder, you have to set the mime type. See the following code for e.g.,
If this scheme of scopes don't match your requirements you can alternatively use the Google Drive REST APIs - Read on - https://stackoverflow.com/a/25871516/1363471