Google Drive Android API – Invalid DriveId and Null ResourceId
I’ve struggled with this code all day but with bad luck. I started following this code Sample from Google.
The problem is that the folder is created successfully, but in onResult() I always get an invalid or incomplete DriveId or resourceId. This means that I can’t create files in the folder I created. This is the code I’m using :
public class CreateFolderActivity extends BaseDemoActivity {
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("MyAppFolder").build();
Drive.DriveApi.getRootFolder(getGoogleApiClient()).createFolder(
getGoogleApiClient(), changeSet).setResultCallback(callback);
}
final ResultCallback<DriveFolderResult> callback = new ResultCallback<DriveFolderResult>() {
@Override
public void onResult(DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the folder");
return;
}
this value is always invalid with ending == operators
Log.d("DRIVEID", "Created a folder: " + result.getDriveFolder().getDriveId());
}
};
}
Whenever I run this code, I get the following incomplete id:
CAESABi2VyCCzdWOuVMoAQ==
I don’t know what’s going on here!
I googled and read about adding listeners to listen for completion events, but none of them seem to work.
I’ve seen almost similar issues on SO, but none of them worked for me.
I manually copied the FolderId through the browser after the app was created and then pasted into my android code, and the app successfully created a file. But that’s not how things are supposed to work.
Should I wait for the sync to complete? If yes, what to do?
Thanks in advance!
Solution
The answer to your question may be found The DriveId you get is fine, but you shouldn’t deal with it directly. It is a “preliminary” DriveId that changes after the object is committed (see again SO 22874657 )。 You can test it and compare the DriveId you get with the DriveId you will get in onCompletion.
This is just one of the side effects of GDAA logic, which insulates you from online/offline network states, which can cause unpredictable delays. You just need to rely on callbacks.
But to my surprise, you can’t immediately use this “preliminary” DriveId (if it’s a folder) as the parent of another object (folder/file). I’ve never experienced passing a ‘preliminary’ DriveId to another GDAA method at once.
The situation with ResourceId is different. That one is secondary in GDAA and is only used when you leave the device. GDAA does not know about the object until it is submitted (uploaded).
I used similar logic in this demo (see the MainActivity.createTree() method). You are welcome to dig deeper.
SO Related issues are discussed in 34318220.
Good luck