Google云端硬盘无法找到文件夹或文件

时间:2017-02-02 23:47:34

标签: android google-drive-android-api

我的应用可以将字符表保存到Google云端硬盘。它创建了一个名为" SWChars"的文件夹。在此人的Google云端硬盘的根目录上,然后将文件保存在该文件夹中。每个文件等于一个字符。当运行以下内容时,它只查找它创建的文件夹。如果您需要更多上下文,可以在Github

找到完整的源代码

以下是在GoogleApiClient的onConnected方法上完成的:

public static void connect(final Activity main){
    final DriveFolder root = Drive.DriveApi.getRootFolder(((SWrpg)main.getApplication()).gac);
    root.queryChildren(((SWrpg)main.getApplication()).gac,new Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE,"SWChars")).build())
            .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(@NonNull final DriveApi.MetadataBufferResult metBufRes) {
                    if(metBufRes.getStatus().isSuccess()){
                        final MetadataBuffer metBuf = metBufRes.getMetadataBuffer();
                        final DriveFolder[] charsFold = {null};
                        System.out.println("Root List Size: "+String.valueOf(metBuf.getCount()));
                        for(Metadata met:metBuf){
                            if(met.isFolder() && !met.isTrashed()){
                                charsFold[0] = met.getDriveId().asDriveFolder();
                                break;
                            }
                        }
                        metBuf.release();
                        if(charsFold[0] ==null){
                            System.out.println("Creating Chars Folder");
                            root.createFolder(((SWrpg)main.getApplication()).gac,
                                    new MetadataChangeSet.Builder().setTitle("SWChars").build())
                                    .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                                        @Override
                                        public void onResult(@NonNull DriveFolder.DriveFolderResult driveFolderResult) {
                                            if(driveFolderResult.getStatus().isSuccess()){
                                                charsFold[0] = driveFolderResult.getDriveFolder();
                                                charsFold[0].queryChildren(((SWrpg)main.getApplication()).gac,new Query.Builder()
                                                        .addFilter(Filters.eq(SearchableField.TITLE,"SWShips")).build())
                                                        .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                                                            @Override
                                                            public void onResult(@NonNull DriveApi.MetadataBufferResult metadataBufferResult) {
                                                                if(metadataBufferResult.getStatus().isSuccess()) {
                                                                    MetadataBuffer metBuffer = metadataBufferResult.getMetadataBuffer();
                                                                    final DriveFolder[] shipFold = {null};
                                                                    for (Metadata met:metBuffer){
                                                                        if(met.isFolder()&&!met.isTrashed()){
                                                                            shipFold[0] = met.getDriveId().asDriveFolder();
                                                                            break;
                                                                        }
                                                                    }
                                                                    metBuf.release();
                                                                    if(shipFold[0] ==null){
                                                                        charsFold[0].createFolder(((SWrpg)main.getApplication()).gac,
                                                                                new MetadataChangeSet.Builder().setTitle("SWShips").build())
                                                                                .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                                                                                    @Override
                                                                                    public void onResult(@NonNull DriveFolder.DriveFolderResult driveFolderResult) {
                                                                                        if(driveFolderResult.getStatus().isSuccess()){
                                                                                            shipFold[0] = driveFolderResult.getDriveFolder();
                                                                                            ((SWrpg)main.getApplication()).charsFold = charsFold[0];
                                                                                            ((SWrpg)main.getApplication()).vehicFold = shipFold[0];
                                                                                            System.out.println("Done");
                                                                                        }
                                                                                    }
                                                                                });
                                                                    }
                                                                }
                                                                metadataBufferResult.release();
                                                            }
                                                        });
                                            }
                                        }
                                    });
                        }else{
                            charsFold[0].queryChildren(((SWrpg)main.getApplication()).gac,new Query.Builder()
                                    .addFilter(Filters.eq(SearchableField.TITLE,"SWShips")).build())
                                    .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                                        @Override
                                        public void onResult(@NonNull DriveApi.MetadataBufferResult metadataBufferResult) {
                                            if(metadataBufferResult.getStatus().isSuccess()){
                                                MetadataBuffer mets = metadataBufferResult.getMetadataBuffer();
                                                DriveFolder vehic = null;
                                                for (Metadata met:mets){
                                                    if(met.isFolder()&&!met.isTrashed()){
                                                        vehic = met.getDriveId().asDriveFolder();
                                                        break;
                                                    }
                                                }
                                                mets.release();
                                                if(vehic == null){
                                                    charsFold[0].createFolder(((SWrpg)main.getApplication()).gac,new MetadataChangeSet.Builder()
                                                            .setTitle("SWShips").build())
                                                            .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                                                                @Override
                                                                public void onResult(@NonNull DriveFolder.DriveFolderResult driveFolderResult) {
                                                                    if(driveFolderResult.getStatus().isSuccess()){
                                                                        ((SWrpg)main.getApplication()).vehicFold = driveFolderResult.getDriveFolder();
                                                                        ((SWrpg)main.getApplication()).charsFold = charsFold[0];
                                                                    }
                                                                }
                                                            });
                                                }else{
                                                    ((SWrpg)main.getApplication()).vehicFold = vehic;
                                                    ((SWrpg)main.getApplication()).charsFold = charsFold[0];
                                                }
                                            }
                                            metadataBufferResult.release();
                                        }
                                    });
                        }
                        metBufRes.release();
                    }
                }
            });
}

对于凌乱而感到抱歉。

2 个答案:

答案 0 :(得分:0)

您可能希望使用DriveFolder.listChildren来提供直接子女列表,如working with folders中使用Google云端硬盘Android API所述

此外,在Querying for Files

  

您可以使用com.google.android.gms.drive.query软件包在用户的云端硬盘帐户中搜索其元数据符合搜索条件的文件。您可以为特定文件夹或整个文件系统发出查询。

但请注意:

  

Android Drive API仅适用于https://www.googleapis.com/auth/drive.file范围。这意味着只有用户使用您的应用程序打开或创建的文件才能与查询匹配。

答案 1 :(得分:0)

经过几个小时的浏览后,Google搜索了我的问题的不同变体,我终于找到了答案:https://stackoverflow.com/a/27927026/7180534

使其发挥作用的主要原因是调用Drive.DriveApi.requestSync

新守则:

public static void connect(final Activity main){
    Drive.DriveApi.requestSync(((SWrpg)main.getApplication()).gac).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if(status.isSuccess()){
                final DriveFolder root = Drive.DriveApi.getRootFolder(((SWrpg)main.getApplication()).gac);
                root.queryChildren(((SWrpg)main.getApplication()).gac,new Query.Builder()
                        .addFilter(Filters.eq(SearchableField.TITLE,"SWChars")).setSortOrder(new SortOrder.Builder()
                                .addSortDescending(SortableField.MODIFIED_DATE).build()).build())
                        .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                            @Override
                            public void onResult(@NonNull final DriveApi.MetadataBufferResult metBufRes) {
                                if(metBufRes.getStatus().isSuccess()){
                                    final MetadataBuffer metBuf = metBufRes.getMetadataBuffer();
                                    final DriveFolder[] charsFold = {null};
                                    for(Metadata met:metBuf){
                                        if(met.isFolder() && !met.isTrashed()){
                                            charsFold[0] = met.getDriveId().asDriveFolder();
                                            break;
                                        }
                                    }
                                    metBuf.release();
                                    if(charsFold[0] ==null){
                                        root.createFolder(((SWrpg)main.getApplication()).gac,
                                                new MetadataChangeSet.Builder().setTitle("SWChars").build())
                                                .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                                                    @Override
                                                    public void onResult(@NonNull DriveFolder.DriveFolderResult driveFolderResult) {
                                                        if(driveFolderResult.getStatus().isSuccess()){
                                                            charsFold[0] = driveFolderResult.getDriveFolder();
                                                            charsFold[0].queryChildren(((SWrpg)main.getApplication()).gac,new Query.Builder()
                                                                    .addFilter(Filters.eq(SearchableField.TITLE,"SWShips")).build())
                                                                    .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                                                                        @Override
                                                                        public void onResult(@NonNull DriveApi.MetadataBufferResult metadataBufferResult) {
                                                                            if(metadataBufferResult.getStatus().isSuccess()) {
                                                                                MetadataBuffer metBuffer = metadataBufferResult.getMetadataBuffer();
                                                                                final DriveFolder[] shipFold = {null};
                                                                                for (Metadata met:metBuffer){
                                                                                    if(met.isFolder()&&!met.isTrashed()){
                                                                                        shipFold[0] = met.getDriveId().asDriveFolder();
                                                                                        break;
                                                                                    }
                                                                                }
                                                                                metBuf.release();
                                                                                if(shipFold[0] ==null){
                                                                                    charsFold[0].createFolder(((SWrpg)main.getApplication()).gac,
                                                                                            new MetadataChangeSet.Builder().setTitle("SWShips").build())
                                                                                            .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                                                                                                @Override
                                                                                                public void onResult(@NonNull DriveFolder.DriveFolderResult driveFolderResult) {
                                                                                                    if(driveFolderResult.getStatus().isSuccess()){
                                                                                                        shipFold[0] = driveFolderResult.getDriveFolder();
                                                                                                        ((SWrpg)main.getApplication()).charsFold = charsFold[0];
                                                                                                        ((SWrpg)main.getApplication()).vehicFold = shipFold[0];
                                                                                                        System.out.println("Done");
                                                                                                    }
                                                                                                }
                                                                                            });
                                                                                }
                                                                            }
                                                                            metadataBufferResult.release();
                                                                        }
                                                                    });
                                                        }
                                                    }
                                                });
                                    }else{
                                        charsFold[0].queryChildren(((SWrpg)main.getApplication()).gac,new Query.Builder()
                                                .addFilter(Filters.eq(SearchableField.TITLE,"SWShips")).build())
                                                .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
                                                    @Override
                                                    public void onResult(@NonNull DriveApi.MetadataBufferResult metadataBufferResult) {
                                                        if(metadataBufferResult.getStatus().isSuccess()){
                                                            MetadataBuffer mets = metadataBufferResult.getMetadataBuffer();
                                                            DriveFolder vehic = null;
                                                            for (Metadata met:mets){
                                                                if(met.isFolder()&&!met.isTrashed()){
                                                                    vehic = met.getDriveId().asDriveFolder();
                                                                    break;
                                                                }
                                                            }
                                                            mets.release();
                                                            if(vehic == null){
                                                                charsFold[0].createFolder(((SWrpg)main.getApplication()).gac,new MetadataChangeSet.Builder()
                                                                        .setTitle("SWShips").build())
                                                                        .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                                                                            @Override
                                                                            public void onResult(@NonNull DriveFolder.DriveFolderResult driveFolderResult) {
                                                                                if(driveFolderResult.getStatus().isSuccess()){
                                                                                    ((SWrpg)main.getApplication()).vehicFold = driveFolderResult.getDriveFolder();
                                                                                    ((SWrpg)main.getApplication()).charsFold = charsFold[0];
                                                                                }
                                                                            }
                                                                        });
                                                            }else{
                                                                ((SWrpg)main.getApplication()).vehicFold = vehic;
                                                                ((SWrpg)main.getApplication()).charsFold = charsFold[0];
                                                            }
                                                        }
                                                        metadataBufferResult.release();
                                                    }
                                                });
                                    }
                                    metBufRes.release();
                                }
                            }
                        });
            }
        }
    });
}