This API is deprecated. Clients must migrate to the Drive REST API or another suitable solution to avoid disruptions to your application.

Working with Folders

Folders in the Drive API for Android are specialized resources with metadata and a DriveId. They have a number of additional methods designed to help with common folder operations.

Get the root folder

The root folder is a normal folder which can be retrieved using the DriveResourceClient.getRootFolder() method. It can then be used like any other folder.

Create a folder

To create a folder, call DriveResourceClient.createFolder(), passing in a reference to the parent folder and the metadata object containing the title and other attributes to set the values for the folder.

The following code sample demonstrates how to create a new folder in the root folder:

drive/demos/app/src/main/java/com/google/android/gms/drive/sample/demo/CreateFolderActivity.java
private void createFolder() {
    getDriveResourceClient()
            .getRootFolder()
            .continueWithTask(task -> {
                DriveFolder parentFolder = task.getResult();
                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                                      .setTitle("New folder")
                                                      .setMimeType(DriveFolder.MIME_TYPE)
                                                      .setStarred(true)
                                                      .build();
                return getDriveResourceClient().createFolder(parentFolder, changeSet);
            })
            .addOnSuccessListener(this,
                    driveFolder -> {
                        showMessage(getString(R.string.file_created,
                                driveFolder.getDriveId().encodeToString()));
                        finish();
                    })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to create file", e);
                showMessage(getString(R.string.file_create_error));
                finish();
            });
}

Create file inside a folder

You can also create files inside of folders using the DriveResourceClient.createFile() method. This process the same as creating files in the root folder. You can also pass metadata and contents for the file to the createFile() method.

Search a folder's contents

In addition to listing direct children, you can retrieve the collection of the metadata for the files and folders within a folder using the DriveResourceClient.queryChildren() method.

drive/demos/app/src/main/java/com/google/android/gms/drive/sample/demo/QueryFilesInFolderActivity.java
        Task<MetadataBuffer> queryTask = getDriveResourceClient().queryChildren(folder, query);
        // END drive_android_query_children]
        queryTask
                .addOnSuccessListener(this,
                        metadataBuffer -> mResultsAdapter.append(metadataBuffer))
                .addOnFailureListener(this, e -> {
                    Log.e(TAG, "Error retrieving files", e);
                    showMessage(getString(R.string.query_failed));
                    finish();
                });
    }
}

Store application specific files in the App Folder

The App Folder is a special folder that is hidden from users and other applications. It allows you to store files that users should not modify, such as a configuration files. For more information, read Storing Application Data.

Send feedback about...

Drive API for Android (Deprecated)
Drive API for Android (Deprecated)
Need help? Visit our support page.