0

I'm trying to show the list of files in my google drive, but don't show nothing or only "App data".

This part of code is for to connect with google api , It seems that It's work correctly:

 private void connectGoogleDriveApi(){
    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.baseActivity)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER) 
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    // Connect the client. Once connected, the camera is launched.
    mGoogleApiClient.connect();
}

 @Override
public void onConnected(Bundle bundle) {

    Utils.saveInfo("GoogleApiClass", "API client connected.");
    try{
        //when connect find the files
        listAllFiles();
        showDriveList();

    }catch (Exception e){
        Utils.saveError("GoogleApiClass-->onConnected", e.getMessage(), e);
    }

}

Then I'm trying recovery the data:

  private void listAllFiles() throws Exception{
    Utils.saveInfo("GoogleApiClass", "List all files from drive");

    String rootFolder= Drive.DriveApi.getRootFolder(this.mGoogleApiClient).getDriveId().toString();
    Drive.DriveApi.fetchDriveId(this.mGoogleApiClient, rootFolder)
            .setResultCallback(idCallback);

}

final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
    @Override
    public void onResult(DriveApi.DriveIdResult result) {
        if (!result.getStatus().isSuccess()) {
            Utils.saveInfo("GoogleApiClass-->idCallback","Cannot find DriveId. Are you authorized to view this file?");
            return;
        }
        DriveId driveId = result.getDriveId();
        DriveFolder folder = driveId.asDriveFolder();
        folder.listChildren(mGoogleApiClient)
                .setResultCallback(metadataResult);
    }
};

final private ResultCallback<DriveApi.MetadataBufferResult> metadataResult = new
        ResultCallback<DriveApi.MetadataBufferResult>() {
            @Override
            public void onResult(DriveApi.MetadataBufferResult result) {
                if (!result.getStatus().isSuccess()) {
                    Utils.saveInfo("GoogleApiClass-->idCallback","Problem while retrieving files");
                    return;
                }
                mResultsAdapter.clear();
                mResultsAdapter.append(result.getMetadataBuffer());
                Utils.saveInfo("GoogleApiClass-->idCallback","Successfully listed files.");
            }
        };

Any idea?

Thanks in advance.

  • 1
    possible Duplicate [SO ticket.](http://stackoverflow.com/questions/34253889/list-all-files-in-drive) – KENdi Mar 20 '16 at 15:00
  • You are using the file scope which only allows "Per-file access to files created or opened by the app". Unfortunately, the Android API does not support the full drive scope, so you might want to look into Drive REST API. (see more here: https://developers.google.com/drive/v2/web/scopes) – Anatoli Mar 20 '16 at 22:23

0 Answers0