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

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

我使用新的 Google Drive API 但无法从我的 Google Drive 获取所有文件夹,我只能获取使用 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 但如何设置??

谢谢

最佳答案

根据设计,GDAA仅支持 FILE 范围,即它将仅查找/列出由 Android 应用程序创建的文件夹/文件。

有两种解决方法:

  1. 使用 intents 之一 GDAA,基本上让用户选择文件/文件夹。这样,您的应用程序就可以使用它了。
  2. 使用不同的 API,REST Api,支持 DRIVE 范围,为您的应用程序提供全套文件/文件夹。

如果您想研究这两个 API 的行为方式,我在 Github 上放了两个不同的演示(RESTGDAA CRUD 演示包装器)。

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

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

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

祝你好运

关于java - 在Android上获取所有文件夹google drive api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32760028/

相关文章:

java - 攻击者可以将 sql 或 javascript 注入(inject) java POJO 中吗?

java - 将 int 值赋给字符串

java - 检查 WebElement 对象中是否存在元素

android - 当应用程序处于后台状态 React native 时深度链接不起作用

java - Gson 错误需要 begin_object 但在第 1 行第 1 列路径 $ 处是字符串

javascript - Google Drive API - 复制现有的电子表格文件并使用 JavaScript Client API 设置 "title"

Java/MySQL - 从最后保存的记录中检索 ID

android - 如何用 MVP 实现 PhoneStateListener

javascript - 定期从 Google Drive Trash 中删除特定类型的所有文件

javascript - 谷歌驱动器 API : Correct way to upload binary files via the Multipart API