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

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

问题描述

我试图弄清楚如何使用新的 Google 检查 Google 云端硬盘中是否存在文件夹驱动 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));

如果我尝试在从上述代码中获得的文件夹中创建一个文件,它也不会崩溃吗?显然,我很难理解这个新 API 是如何一起工作的,尤其是在非常有限的教程和 SO 问题的情况下,我真的坚持这个,所以任何输入都将不胜感激.

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.

只是为了澄清我的问题:我正在指定的 Google Drive 文件夹中创建一个文件,但如果该文件夹不存在(已被用户删除),我想先创建它.

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.

推荐答案

经过大量研究,这是我最终得到的代码.它工作正常,但有一个问题:当一个文件夹在 Google Drive 中被丢弃时,我可以从我的应用程序中获取的元数据需要一些时间(小时)才能更新,这意味着此代码可以首先检测文件夹是否已被丢弃几个小时后,垃圾事件实际发生了 - 可以在这里找到更多信息和讨论.

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

            }
        };
}

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

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