在Android上获取所有文件夹google drive api

时间:2015-09-24 11:20:50

标签: java android google-drive-api google-drive-android-api

我使用新的Google云端硬盘api而我无法从我的google云端硬盘获取所有文件夹,我只获得了使用google drive api创建的文件夹... 有人知道为什么会这样吗?

这是我的代码:

@Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            // Create the API client and bind it to an instance variable.
            // We use this instance as the callback for connection and connection
            // failures.
            // Since no account name is passed, the user is prompted to choose.
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        // Connect the client. Once connected, the camera is launched.
        mGoogleApiClient.connect();
    }


    /**
     * Handles resolution callbacks.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
            mGoogleApiClient.connect();

        }
    }

    /**
     * Called when activity gets invisible. Connection to Drive service needs to
     * be disconnected as soon as an activity is invisible.
     */
    @Override
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

    /**
     * Called when {@code mGoogleApiClient} is connected.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "GoogleApiClient connected");

        rootFolder = Drive.DriveApi.getRootFolder(mGoogleApiClient);

       rootFolder.listChildren(getGoogleApiClient()).setResultCallback(pruebaChildren);

    }


    ResultCallback<DriveApi.MetadataBufferResult> pruebaChildren = new
            ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
                    if (!metadataBufferResult.getStatus().isSuccess()) {
                        showMessage("Problem while retrieving files");
                        return;
                    }
                    Log.i(TAG,"got root folder");
                    MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
                    Log.i(TAG,"Buffer count  " + buffer.getCount());
                    if(buffer.getCount() == 0){
                        createFolderApp();
                    }
                    else{
                        for(Metadata m : buffer){
                            Log.i(TAG,"Metadata name  " + m.getTitle() + "(" + (m.isFolder() ? "folder" : "file") + ")");
                            if (m.isFolder() && m.getTitle().equals(TAG)){
                                Log.i(TAG,"APP FOLDER FOUND");
                                Drive.DriveApi.getFolder(mGoogleApiClient, m.getDriveId())
                                        .listChildren(mGoogleApiClient)
                                        .setResultCallback(foreachAppAplication);
                            }
                        }
                    }
                    return;
                }
            };

现在我想查看rootFolder Drive中的所有文件夹,我尝试requestSync()但结果是一样的......我需要帮助!

另一个问题:如何设置AppFolder?我只看到getAppFolder但我怎么设置?

谢谢

1 个答案:

答案 0 :(得分:6)

按照设计,GDAA仅支持FILE范围,即它只会查找/列出Android应用程序创建的文件夹/文件。

有两种方法:

  1. 使用GDAA的 intents 之一,基本上让用户选择文件/文件夹。这样,它就可以用于您的应用。
  2. 使用支持DRIVE范围的其他API REST Api,为您的应用提供全套文件/文件夹。
  3. 如果你想研究两个API的行为方式,我在Github上放了两个不同的演示版(RESTGDAA CRUD演示包装器。)

    你问题的第二部分没有答案。你没有设置app文件夹,你只能得到它的DriveFolder id。您可以使用它来创建/检索对象。

      DriveFolder appFldr = Drive.DriveApi.getAppFolder(mGooleApiClient);
      appFldr.createFile(...);
      appFldr.createFolder(...);
      appFldr.listChildren(...);
      appFldr.queryChildren(...);
    

    ...不要忘记添加 SCOPE_APPFOLDER 范围

    祝你好运