谷歌推动Android API - 检查文件夹是否存在 [英] Google Drive Android API - Check if folder exists

查看:156
本文介绍了谷歌推动Android API - 检查文件夹是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何检查使用新的谷歌推动Android API

I'm trying to figure out how to check if a folder exists in Google Drive using the new Google Drive Android API

我已经试过以下,以为这样就可以要么崩溃或如果找不到该文件夹​​返回null,但它并没有做到这一点(只是只要它是一个有效的DriveId 时,即使在文件夹已被删除)。

I've tried the following, thinking that it would either crash or return null if the folder is not found, but it doesn't do that (just as long as it is a valid DriveId, even though the folder has been deleted).

DriveFolder folder = Drive.DriveApi.getFolder(getGoogleApiClient(), driveId));

如果我尝试创建一个文件,我从上面的code得到的文件夹,它不会崩溃要么? 我显然有一点很难理解怎么这个新的API的工作都在一起,特别是在非常有限的教程和SO问题在那里,我真的卡在这一块,所以任何输入将大大AP preciated。

If i try to create a file the folder I get from the above code, it does not crash either? I'm clearly having a little hard time understanding how this new API works all to together, especially with the very limited tutorials and SO questions out there, and I'm really stuck on this one, so any input will be much appreciated.

只是为了澄清我的问题:我创建了指定的谷歌驱动器文件夹中的文件,但如果该文件夹不存在(已被删除的用户),我想先创建它。

Just to clarify my problem: I'm creating a file in a specified Google Drive folder, but if the folder does not exist (has been deleted by user), I want to create it first.

推荐答案

大量的研究后,这将是code我结束了。它工作正常,但有一个问题:当一个文件夹被丢弃在谷歌驱动器需要一定的时间(小时)元数据之前,我可以从我的应用程序获取更新,这意味着此code可以先检测是否夹有被捣毁了几个小时后,捣毁事件实际发生 - 更多的信息和讨论都可以在这里找到

After a lot of research this is the code I ended up with. It works properly, but has an issue: When a folder is trashed in Google Drive it takes some time (hours) before the metadata I can fetch from my app is updated, meaning that this code can first detect if the folder has been trashed a couple of hours later the trashing event actually happened - further information and discussions can be found here.

public class checkFolderActivity extends BaseDemoActivity {

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);

        DriveId folderId = DriveId.decodeFromString(folderId);
        DriveFolder folder = Drive.DriveApi.getFolder(mGoogleApiClient, folderId);
        folder.getMetadata(mGoogleApiClient).setResultCallback(metadataRetrievedCallback);

    }

     final private ResultCallback<DriveResource.MetadataResult> metadataRetrievedCallback = new
        ResultCallback<DriveResource.MetadataResult>() {
            @Override
            public void onResult(DriveResource.MetadataResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.v(TAG, "Problem while trying to fetch metadata.");
                    return;
                }

                Metadata metadata = result.getMetadata();
                if(metadata.isTrashed()){
                    Log.v(TAG, "Folder is trashed");
                }else{
                    Log.v(TAG, "Folder is not trashed"); 
                }

            }
        };
}

这篇关于谷歌推动Android API - 检查文件夹是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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