Android Google DriveApi在不同设备上针对同一标题返回不同的DriveID [英] Android Google DriveApi returning different DriveID for same title on different devices

查看:77
本文介绍了Android Google DriveApi在不同设备上针对同一标题返回不同的DriveID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,首先,我知道每个文件/文件夹的DriveID都是特定于设备的,但是resourceId是恒定的,这就是我用来测试各项是否相同(当然,标题除外)的真正原因.

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).

问题

我正在尝试跨设备同步AppFolder中的文件夹,但是,尽管在执行任何操作之前通过标题进行查询,但我在每台设备上都获得了不同的文件/文件夹.所以一些代码:

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:

我如何设置api客户端

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();

我如何设置appFolder

How I set up the appFolder

appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient);

我已验证我在两台设备上都获得了与AppFolder相同的resourceId.

我如何创建/获取文件夹

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();
    }
}

在此方面,我始终能获得预期的结果. IE. Found 1 existing folders,然后Returning existing folder.另外,在您提到使用await()之前,我正在剥离自己的线程,并且从来没有在UI线程上调用它,所以不用担心.

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

我注意到sqlite db没有被复制,然后让我弄清楚甚至没有文件夹都在交叉,所以我写了另一个测试:

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

,另一个:

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

我已经验证4次以上,这些设备正在使用相同的Google帐户.如果还有其他代码或信息可以帮助您,请告诉我!任何帮助找到答案的帮助将不胜感激^^谢谢!

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!

推荐答案

不幸的是,我在您的具体案例说明中迷路了,但是如果我只阅读标题,由于遇到了同样的问题,我可以给您一些线索.

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.

GDAA(Google Drive Android Api)中的每个对象(文件夹或文件)都有2个ID,分别是DRIVE_ID和RESOURCE_ID.

There are 2 ID's for each object (folder, or file) in GDAA (Google Drive Android Api), the DRIVE_ID and the RESOURCE_ID.

DRIVE_ID标识符特定于您的特定设备,因此,如果在其他设备上使用它,则没有任何意义. RESOURCE_ID在设备之间是唯一的,因此这是您用于任何类型的同步所需要的.参见 SO 22841237 .

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.

如果您使用GDAA创建文件/文件夹,则RESOURCE_ID可能不会立即可用,因为GDAA需要提交"对象以获取它.参见 SO 22432431 .

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.

完全不要使用文件/文件夹标题(名称),因为它不是Google云端硬盘世界中的主要ID.您可以在同一区域中具有相同标题(名称)的多个云端硬盘对象.

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).

另外,请注意,在方程式中添加了GDAA(与使用普通 SO 23073474 .

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.

在给您一个提示后,我花了几个月的时间解决了同样的问题,然后回过头来使用数据提供程序和同步适配器的RESTful API,对Drive进行了轮询.下一步将添加基于GCM的同步.我希望同步(使用GCM)将内置在GDAA中.但这显然不是.而且您还没有尝试删除文件/文件夹,因此

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.

祝你好运.

这篇关于Android Google DriveApi在不同设备上针对同一标题返回不同的DriveID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆