You can create files in two ways with the Drive Android API: using the
DriveClient.newCreateFileActivityIntentSender
method and the
CreateFileActivityOptions
class; or using the
DriveResourceClient.createFile
method.
The end result of either approach is the same.
However, for instances where the user is specifying
the location or title of the file,
DriveClient.newCreateFileActivityIntentSender
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 createFile()
method.
When you create a file through either method, you can specify the initial
set of metadata, file contents, and the parent
folder. In the case of using DriveClient.newCreateFileActivityIntentSender
,
the user can subsequently modify the parent folder through the UI.
Creating files with the CreateFileActivityOptions
class
The
DriveClient.newCreateFileActivityIntentSender
method and the
CreateFileActivityOptions
class provide 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 initial 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:
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 DriveResourceClient.createFile()
method.
When you call DriveResourceClient.createFile()
, the Drive for Android API
places the new file in the folder that you passed in as an argument.
The following example demonstrates creating
a new text file in the user's root folder:
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.
If you are using the
DriveClient.newCreateFileActivityIntentSender
method and the
CreateFileActivityOptions
class to create an empty file, pass
null
in place of DriveContents
to the setInitialContents
method.
If you are creating empty files programmatically, pass null
in place of
DriveContents
to the createFile
method: