Creating Files

You can create files in two ways with the Drive Android API: using the CreateFileActivityBuilder class, or using the createFile() method of the DriveFolder interface. The end result of either approach is the same. However, for instances where the user is specifying the location or title of the file, CreateFileActivityBuilder simplifies the process by providing a premade user interface. To create files programmatically or allow users to specify other metadata in a custom UI activity, you must use the the createFile() method.

When a file is created through either method, you can specify the initial set of metadata, file contents, and the parent folder. In the case of using CreateFileActivityBuilder, the user can subsequently modify the parent folder through the UI.

Creating files with the CreateFileActivityBuilder class

The CreateFileActivityBuilder class provides a convenient way to create new files and allow the user to specify the destination folder and title for the file. You can also use this class to specify the intial metadata and contents for the file.

The class creates an intent that you can use to start the create file activity containing the UI. The following example demonstrates how to use the activity builder to create a text file:

ResultCallback<DriveContentsResult> contentsCallback = new
        ResultCallback<DriveContentsResult>() {
    @Override
    public void onResult(DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            // Handle error
            return;
        }

        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                .setMimeType("text/html").build();
        IntentSender intentSender = Drive.DriveApi
                .newCreateFileActivityBuilder()
                .setInitialMetadata(metadataChangeSet)
                .setInitialDriveContents(result.getDriveContents())
                .build(getGoogleApiClient());
        try {
            startIntentSenderForResult(intentSender, 1, null, 0, 0, 0);
        } catch (SendIntentException e) {
            // Handle the exception
        }
    }
}

For a full working example, see the CreateFileWithCreatorActivity sample in the Google Drive Android API Demos app.

Creating files programmatically

To create multiple files programmatically, or if you prefer to use your own UI to let users specify additional metadata, you must use the createFile() method in the DriveFolder class.

The new file is placed in the folder corresponding to the DriveFolder object you use to create the file. The following example demonstrates creating a new text file in the user's root folder:

public class CreateFileActivity {

    // ...

    ResultCallback<DriveContentsResult> contentsCallback = new
            ResultCallback<DriveContentsResult>() {
        @Override
        public void onResult(DriveContentsResult result) {
            if (!result.getStatus().isSuccess()) {
                // Handle error
                return;
            }

            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("New file")
                    .setMimeType("text/plain").build();
            // Create a file in the root folder
            Drive.DriveApi.getRootFolder(getGoogleApiClient())
                    .createFile(getGoogleApiClient(), changeSet, result.getDriveContents())
                    .setResultCallback(this);
        }
    }
}

For a full working example, see the CreateFileActivity sample in the Google Drive Android API Demos app.

Creating empty files

You may sometimes need to create empty files, for example, a user preference file that is empty until the user defines some application property or a plain text file that is empty until some content is written to it later on. To create an empty file, create a file as usual but passing null where you would normally pass DriveContents. This generates an empty DriveContents for you.

If you are using the CreateFileActivityBuilder to create an empty file, pass null in place of DriveContents to the setInitialContents method:

IntentSender intentSender = Drive.DriveApi
        .newCreateFileActivityBuilder()
        .setInitialMetadata(metadataChangeSet)
        .setInitialContents( null /* DriveContents */)
        .build(getGoogleApiClient());

If you are creating empty files programmatically, pass null in place of DriveContents to the createFile method:

Drive.DriveApi.getRootFolder(getGoogleApiClient())
        .createFile(getGoogleApiClient(), changeSet, null /* DriveContents */)
        .addResultCallback(this);

发送以下问题的反馈:

此网页
Drive API for Android
Drive API for Android