问题
So first of all I know that the DriveID for each file/folder is device specific, BUT the resourceId is constant and that's really what I'm using to test if the items are the same (besides the title of course).
Problem
I am trying to sync folders in my AppFolder across devices, however, I'm getting different files/folders on each device despite querying via the title before doing anything. So some code:
How I set up the api client
mGoogleApiClient = new GoogleApiClient.Builder(context.getApplicationContext())
.addApi(Drive.API)
.addApi(Plus.API)//used for logout process
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(OnConnection)
.addOnConnectionFailedListener(OnFailedListener)
.build();
How I set up the appFolder
appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient);
I have verified that I'm getting the same resourceId for the AppFolder on both devices.
How I create/get the folders
private static DriveFolder CheckForOrCreateDriveFolder(DriveFolder appFolder, String folder) {
MetadataBuffer metadataBuffer = appFolder.queryChildren(mGoogleApiClient, new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, folderName)).build()).await().getMetadataBuffer();
if(metadataBuffer != null&&metadataBuffer.getCount()>0) {
Log.v(TAG,"Found "+metadataBuffer.getCount()+" existing folders");
Metadata metadata = metadataBuffer.get(0);
if (metadata != null && metadata.isFolder()) {
Log.v(TAG,"Returning existing folder");
return Drive.DriveApi.getFolder(mGoogleApiClient, metadata.getDriveId());
}else{
Log.v(TAG,"Returning created folder even though we found meta data");
return appFolder.createFolder(mGoogleApiClient, new MetadataChangeSet.Builder().setTitle(folderName).build()).await().getDriveFolder();
}
}else{
Log.v(TAG,"Returning created folder");
return appFolder.createFolder(mGoogleApiClient, new MetadataChangeSet.Builder().setTitle(folderName).build()).await().getDriveFolder();
}
}
On this I always get the expected result. I.E. Found 1 existing folders
and then Returning existing folder
. Also, before you mention the use of await()
I'm spinning off my own threads and this is NEVER called on UI thread, so no worries.
Test code
I noticed that sqlite db wasn't copying over which then let to me figuring out that not even the folders were crossing over so I wrote another test:
DriveApi.MetadataBufferResult metadataBufferResult = appFolder.listChildren(mGoogleApiClient).await();
Log.v(TAG,"AppFolder Contents:");
for (Metadata metadata : metadataBufferResult.getMetadataBuffer()) {
Log.v(TAG,"MD:"+metadata.getTitle()+" : "+metadata.getDriveId().getResourceId()+" - "+(metadata.isFolder()?"is a folder":"is a file"));
}
Which for one device returns:
AppFolder Contents:
MD:itemImage : 1eFvDS6cWmCbgblahblahblahblahblah - is a folder
MD:itemAudio : 1c86N8AGRV8Bblahblahblahblahblah - is a folder
MD:userImage : 1LnsgFneT-l3ZYzMbblahblahblahblah - is a folder
MD:db.sqlite : 1DH4QfUfrsxDScblahblahblahblah - is a file
and for the other:
AppFolder Contents:
MD:itemAudio : 1CqeVASm_Gjjblahblahblahblahblah - is a folder
MD:itemImage : 1Djs059FYGSfOblahblahblahblahblah - is a folder
MD:userImage : 1U5c5yCw-XdfCblahblahblahblahblah - is a folder
MD:db.sqlite : 15qR3WLmvT3Nn2Ztblahblahblahblahblah - is a file
I've verified 4 times or more that these devices are using the same google account. If there is any other code or info I could give you to help please let me know! Any help finding an answer to this would be greatly appreciated^^ Thanks!
回答1:
Unfortunately, I got lost in your specific case explanation, but if I only read the headline, I can give you some clues, since I ran into the same problem.
There are 2 ID's for each object (folder, or file) in GDAA (Google Drive Android Api), the DRIVE_ID and the RESOURCE_ID.
The DRIVE_ID identifier is specific to your particular device, so if you use it on another device, it means nothing. The RESOURCE_ID is unique across devices, so that's the one you need to use for any type of synchronization. See SO 22841237.
If you create a file/folder using GDAA, the RESOURCE_ID may not be immediately available, since GDAA needs to 'commit' the object to get it. See SO 22432431.
DO NOT use file/folder title (name) at all, since it is not a primary ID in the Google Drive world. You can have multiple Drive objects in the same area with the same title (name).
Also, be aware of the fact, that adding GDAA to the equation (as opposed to using plain RESTful API), you are adding a layer that has its own latency you have no control over. The 'ChangeEvent Listener' and 'requestSync()' are no help either. See SO 23073474.
Just to give you a hint, after spending a few months fighting the same issues, I rolled back to the RESTful API, with data provider and sync adapter, polling Drive for changes. The next step will be to add GCM-based synchronization. I was hoping the sync (using GCM) would be built-in the GDAA. But it apparently is not. And you haven't tried to delete files/folders yet, that's where the real fun begins.
Good Luck.
回答2:
Does setting up the appFolder
like this help?
String appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient()).getDriveId().getResourceId().toString();
回答3:
I was having the same issue (duplicated folders) and I solved using requestSync BEFORE running the search query:
Drive.DriveApi.requestSync(getGoogleApiClient()).await();
来源:https://stackoverflow.com/questions/26668447/android-google-driveapi-returning-different-driveid-for-same-title-on-different