Java – Get all folders Google Drive API on Android

Get all folders Google Drive API on Android… here is a solution to the problem.

Get all folders Google Drive API on Android

I

use the new Google Drive API but can’t get all folders from my Google Drive, I can only get folders created with Google Drive API…
Does anyone know why this is so?

Here is my 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;
                }
            };

Now I want to see all folders in rootFolder Drive, I tried requestSync() but the result is the same… I need help!

Another question: How do I set up AppFolder? I only see getAppFolder but how do I set it up??

Thanks

Solution

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

There are two workarounds:

  1. Using one of the intents is GDAA, basically letting the user select a file/folder. That way, your app can use it.
  2. Use different APIs, REST APIs, support DRIVE scope, and provide a full set of files/folders for your application.

If you want to investigate how these two APIs behave, I put two different demos on Github (REST and). GDAA CRUD demo wrapper).

There is no answer to the second part of your question. You don’t set up an application folder, you can only get its DriveFolder id. You can use it to create/retrieve objects.

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

… And don’t forget to add the SCOPE_APPFOLDER range

Good luck

Related Problems and Solutions