Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Drive SDK - how to show list of files in selected folder

I am struggling to get my code to work.

I want my app (when a button is clicked) to show all the folders (and files) in the root folder of the drive. But it seems this is not possible since even though I have authorised the app using Ouath2, the app only has access to the files and folder that the user selects.

The behaviour is strange in that when the folder picker opens, it only lists the contents of that folder if the contents ARE folders. It will not list any files at all.

Could an expert please look at my code and tell me where I am going wrong?

I am using the PickFolderWithOpener activity, in order to get the driveId. It returns a driveID based on the users selection from the picker..

 DriveId driveId = (DriveId) data.getParcelableExtra(
                            OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

Is that is what is returned here, the folder Drive ID? I then need to call the intent ListFilesInFolderActivity. I assume I have to pass this driveID in the intent call? If I format the DriveID as a string and pass it, it fails with the "Cannot find DriveId. Are you authorized to view this file?" message.

So what I am doing is passing the ResourceID.

Intent intent = new Intent(this, ListFilesInFolderActivity.class);
                    intent.putExtra("DriveID", driveId.getResourceId());
                    startActivity(intent);

What needs to be used in the ListFilesInFolderActivity? The DriveID or resourceID? Here is what I have in the ListFilesInFolderActivity?

Bundle extras = getIntent().getExtras();
        folderId= extras.getString("DriveID"); // this is the resourceID
Drive.DriveApi.fetchDriveId(getGoogleApiClient(), folderId)
                .setResultCallback(idCallback);


final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
@Override
public void onResult(DriveIdResult result) {
    if (!result.getStatus().isSuccess()) {
        showMessage("Cannot find DriveId. Are you authorized to view this file?");
        return;
    }
    DriveFolder folder = Drive.DriveApi.getFolder(getGoogleApiClient(), result.getDriveId());
    showMessage("Result driveId is:  " + result.getDriveId());
    folder.listChildren(getGoogleApiClient())
            .setResultCallback(metadataResult);



 }
};

Which seems to work, but it only lists folders in the selected folder. It does not list the files. Why does folder.listChildren(getGoogleApiClient()) not list the files also!?

Hopefully this is something obvious I am missing.

like image 569
user3437721 Avatar asked Apr 04 '14 09:04

user3437721


People also ask

How do I get a list of files in a Google Drive folder?

Goto the same Google Sheets File and refresh the page. Then you will get the "List Files/Folders" menu, click on it, and select the "List All Files and Folders" item.

How do I view files in Google Drive as a list?

To switch from grid view to list view, open Google Drive and press the List view button. Note: Smartbar features are available in the Google Drive file preview for Google files and non-Google files. You can access the file preview directly from grid mode.

What is root folder in Google Drive?

Each user has a "root" folder called "My Drive" that functions as their primary hierarchy, and consists of everything that descends from this root folder. The user is the primary owner of this folder. Shared drives. A shared drive is an organizational structure within Google Drive that lives parallel to My Drive.


1 Answers

You cannot do this with the new SDK as it only allows SCOPE_FILE access.

Is this why?? "Note: The Google Drive Android API currently only supports drive.file and drive.appdata authorization scopes. If your application requires additional permissions or features not yet available in the Drive Android API, you must use the Google APIs Java Client."

like image 69
user3437721 Avatar answered Sep 21 '22 14:09

user3437721