Storing app data

The Google Drive API includes a special hidden folder that your app can use to store application specific data.

What is the App Folder?

The App Folder is a special folder that is only accessible by your application. Its content is hidden from the user and from other apps. Despite being hidden from the user, the App Folder is stored on the user's Drive and therefore uses the user's Drive storage quota. The App Folder can be used to store configuration files, temporary files, or any other types of files that belong to the user but should not be tampered with.

Even though users can't see the App Folder's content, they can see the amount of storage used by your App Folder in the Manage Apps dialog:

Manage apps dialog showing Application Data folder size

Your App Folder is deleted if users uninstall your app from their Drive. They can also delete your app's App Folder manually using the Options menu in the Manage Apps dialog.

Get authorization to use the App Folder

To be able to use your App Folder, your application must request the SCOPE_APPFOLDER scope as part of authorization.

Get the App Folder

The App Folder can be retrieved by using the DriveResourceClient.getAppFolder method. It can then be used similarly to a normal folder. For example, you can use the DriveResourceClient.queryChildren method to list the files in the App Folder.

Check if a file or folder is in the App Folder

You can check whether a file or folder is in the App Folder by retrieving its metadata. The Metadata.isInAppFolder method returns true if the file or folder is in the App Folder.

Create a file in the App Folder

Creating a file in the App Folder is the same as for a normal folder. The following example demonstrates creating a new text file named New File.txt in the App Folder:

private void createFileInAppFolder() {
    final Task<DriveFolder> appFolderTask = getDriveResourceClient().getAppFolder();
    final Task<DriveContents> createContentsTask = getDriveResourceClient().createContents();
    Tasks.whenAll(appFolderTask, createContentsTask)
            .continueWithTask(new Continuation<Void, Task<DriveFile>>() {
                @Override
                public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {
                    DriveFolder parent = appFolderTask.getResult();
                    DriveContents contents = createContentsTask.getResult();
                    OutputStream outputStream = contents.getOutputStream();
                    try (Writer writer = new OutputStreamWriter(outputStream)) {
                        writer.write("Hello World!");
                    }

                    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                                          .setTitle("New file")
                                                          .setMimeType("text/plain")
                                                          .setStarred(true)
                                                          .build();

                    return getDriveResourceClient().createFile(parent, changeSet, contents);
                }
            })
            .addOnSuccessListener(this,
                    new OnSuccessListener<DriveFile>() {
                        @Override
                        public void onSuccess(DriveFile driveFile) {
                            showMessage(getString(R.string.file_created,
                                    driveFile.getDriveId().encodeToString()));
                            finish();
                        }
                    })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "Unable to create file", e);
                    showMessage(getString(R.string.file_create_error));
                    finish();
                }
            });
}

Send feedback about...

Drive API for Android
Drive API for Android