获取Android上的所有文件夹谷歌驱动的API文件夹、Android、API

2023-09-13 02:07:34 作者:栀阝先生の樹與梦

我用的是新的谷歌驱动的API,我不能让所有的文件夹从我的谷歌驱动器,我只得到了我与谷歌驱动的API创建的文件夹... 任何人都知道为什么会发生这样?

I use the new Google drive api and I can't get All folders from my google drive, I only get the folders that I create with the google drive api... Anybody know why happens this?

这是我的code:

@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驱动器的所有文件夹,我尝试requestSync(),但结果是一样的......我需要帮助,请!

And now I want see all folders in rootFolder Drive, I try the requestSync() but the result is same... I need help please!

和另外一个问题:我怎么可以设置AppFolder?我只看到getAppFolder但我可以怎么设置?

And another question: How I can set the AppFolder? I only see getAppFolder but How I can set ??

感谢

推荐答案

在设计上, GDAA 仅支持文件范围内,也就是说,它会发现在Android应用程序中创建/只列出文件夹/文件。

By design, GDAA supports only the FILE scope, i.e. it will find / list only folders / files created by the Android app.

有它周围的2种方式:

使用的 意图 的的GDAA之一,基本上让用户选择文件/文件夹。这样,它会成为提供给您的应用程序。 使用不同的API,在 REST 的API,它支​​持的驱动器范围,让您的应用程序全套文件/文件夹。 Use one of the intents of the GDAA, basically letting the user pick the file / folder. That way it will become available to your app. Use a different API, the REST Api, which supports the DRIVE scope, giving your app full set of files / folders.

如果你想学习这两个API的行为,我已经把两个不同的演示在Github上(的 REST 和 GDAA CRUD演示包装)。

In case you want to study how the two APIs behave, I've put two different demos on the Github (the REST and the GDAA CRUD demo wrappers).

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

The second part of your question does not have answer. You don't set the app folder, you can only get it's DriveFolder id. You use it to create / retrieve objects.

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

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

好运