user3833717
user3833717

Reputation: 11

List files and folders - Google Drive

I want to list files and folder from google drive, i need to use this code but i don't get how to initialize the Drive class so that i can execute this function. please help me!

private static List<File> retrieveAllFiles(Drive service) throws IOException {
    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {
      try {
        FileList files = request.execute();

        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
      } catch (IOException e) {
        System.out.println("An error occurred: " + e);
        request.setPageToken(null);
      }
    } while (request.getPageToken() != null &&
             request.getPageToken().length() > 0);

    return result;
  }

Upvotes: 0

Views: 339

Answers (1)

sky91
sky91

Reputation: 3160

You can refer this link
https://software.intel.com/en-us/android/articles/connecting-to-google-drive-from-an-android-app

Download the sample source code and debug it.
You can trigger the line at:

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Connect to Google Drive
        mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE));
        startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);

        //bla... bla...
    }

    @Override
        protected void onActivityResult(final int requestCode, final int resultCode, final Intent data)
        {
            switch (requestCode)
            {
            case REQUEST_ACCOUNT_PICKER:
                if(resultCode == RESULT_OK && data != null && data.getExtras() != null)
                {
                    String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                    if(accountName != null)
                    {
                        mCredential.setSelectedAccountName(accountName);
                        mService = getDriveService(mCredential);
                    }
                }
                break;
    }
    }

Upvotes: 0

Related Questions