Create a File on Google Drive

suggest change

This example creates a text file in the user's Drive root. createFile() needs the metadata, the file contents, and the parent folder.

CreateMyFile() asks the Drive API for a DriveContents resource and handles it in driveContentsCallback. That resource is a temporary binary stream only this app can write.

public void CreateMyFile(){
    fileOperation = true;
    // Create new contents resource.
    Drive.DriveApi.newDriveContents(mGoogleApiClient)
                  .setResultCallback(driveContentsCallback);
}

Result Handler of DriveContents

Check result.getStatus() before using the DriveContents.

We will create a result handler of DriveContents. Within this method, we call the CreateFileOnGoogleDrive() method and pass the result of DriveContentsResult:

/**
 * This is the Result result handler of Drive contents.
 * This callback method calls the CreateFileOnGoogleDrive() method.
 */
final ResultCallback<DriveContentsResult> driveContentsCallback =
         new ResultCallback<DriveContentsResult>() {
            @Override
            public void onResult(DriveContentsResult result) {
                if (result.getStatus().isSuccess()) {
                    if (fileOperation) {
                        CreateFileOnGoogleDrive(result);
                    }
                }
            }
        };

Create File Programmatically

A MetadataChangeSet sets the title and MIME type. DriveFolder.createFile() takes the API client, that change set, and the DriveContents. The result callback must be chained with a dot. A newline between createFile(...) and setResultCallback does not compile.

/**
 * Create a file in the root folder using a MetadataChangeSet object.
 * @param result
 */
public void CreateFileOnGoogleDrive(DriveContentsResult result){

    final DriveContents driveContents = result.getDriveContents();

    // Perform I/O off the UI thread.
    new Thread() {
        @Override
        public void run() {
            // Write content to DriveContents.
            OutputStream outputStream = driveContents.getOutputStream();
            Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
            try {
                writer.write("Hello Christlin!");
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
                return;
            } finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage());
                }
            }

            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("My First Drive File")
                    .setMimeType("text/plain")
                    .setStarred(true).build();

            // Create a file in the root folder.
            Drive.DriveApi.getRootFolder(mGoogleApiClient)
                    .createFile(mGoogleApiClient, changeSet, driveContents)
                    .setResultCallback(fileCallback);
        }
   }.start();
}

Handle result of Created File

The following code will create a callback method to handle the result of the created file:

/**
 * Handle result of Created file
 */
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
        ResultCallback<DriveFolder.DriveFileResult>() {
            @Override
            public void onResult(DriveFolder.DriveFileResult result) {
                if (result.getStatus().isSuccess()) {
                    Toast.makeText(getApplicationContext(), "file created: "+
                                result.getDriveFile().getDriveId(), Toast.LENGTH_LONG).show();
                }
                return;
            }
        };

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents