JEN KING : Hi, I'm Jen King, the tech lead on the Drive Android
API.
And today I'm going to show you how
to add drive integration to your Android app.
This API is a part of Google Play services, which
means that you can add a small amount of code to your app
and get the benefits of these API features
on nearly every Android device running Gingerbread or higher.
There are a lot of reasons that Google Drive integration is
great for users.
Their files get automatically backed up to the cloud,
are accessible on multiple devices including the web,
and can be viewed and edited on mobile devices and the web
using Drive-enabled apps, even when offline.
So let's get started.
I'm going to demonstrate how to integrate an app with Drive
using our receipt collector sample app.
This app allows users to take pictures of their receipts
and store them in Drive to manage their spending.
In order to use the Drive Android API,
you first need to register your app as a Drive app
by creating an entry in the Developer Console.
The Console will ask you to specify
the package name of your app and the fingerprint of your signing
certificate so that the Drive API can authenticate your app.
Creating a project ID on the Dev Console
allows you to build versions of your app
across Android, iOS, and the web and share a single project
configuration for all of them.
Once your project is registered, you
can start using the Drive Android
API by including the Google Play services
APIs in your application.
Before integrating with Drive, our app just saves the receipts
to the local file system on the device, which
means they aren't easily accessible on other devices.
If I lose or break my device, the data
might get lost with it.
However, it's easy to back up these receipts
to Drive using the Drive Android API.
To do this, we'll create a new file
with the image bytes using the Drive API, which
will sync the data up to the server.
We need to write the receipt image to a contents object
and create a new Drive file using this contents.
For now, we'll just add all the receipts
to the user's root folder, also called My Drive.
To save the new file, we call createFile on the folder where
we want to insert the file and pass in the contents
object containing the data.
The createFile call will return the Drive ID of the new file.
Receipts will now get automatically uploaded
to the user's Drive, allowing users to view and organize them
in the Drive Web UI.
Next, we need to change our receipt list
to request the list of receipt files
from the Drive API instead of the local file system.
To do this, we'll query for all files in the user's My Drive
folder that are accessible by the receipts app.
This query uses the local Drive database rather than
the server, so it will still work while offline.
The Drive Android API will automatically sync metadata
for files created from other devices.
We'll change our receipt list view
to use data from the Drive API by changing
the adapter in our list view to use the metadata buffer
object returned by the query method.
We use the title field to generate an entry
for each file on the list.
Last, we need to display the receipt image and description
when users open it from the list.
To display the receipt image, get the Drive file
for the receipt using DriveApi.getFile,
and open the contents using DriveFile.openContents.
If the file content is already available on the device,
the Open File method will return immediately.
And if not, the Drive Android API
will fetch the content from the server for you.
Now, the app functions just as it did before
with the added benefit that users
can open all of their receipts on any Android device,
as well as on the web.
There are three more cool features
that can help us to make the user experience better.
Pinning files, the File Picker UI, and app folders.
First, you can allow your users to save receipts
for offline use, or pin the files,
using the Set Pinned API.
This API will ensure that the file content is on the device
and will keep it up to date.
To surface this feature to users,
we'll add a Pin button into our View Receipt
UI, which allows the user to choose
to keep the file on the device.
Second, we're currently saving all receipts
to the user's My Drive folder.
But what if they want to be a little more organized?
We can allow users to choose which
folder to use for saving their receipts by launching the Drive
File Picker.
Once the user selects Folder, we save this preference
to the device.
And every time we create a new receipt,
we create it in the selected folder.
Last, it would be a lot better if this preference were
consistent between multiple devices.
We could store our preferences in a file in Drive
just like we do with receipts, but it
would be better if we didn't clutter
the user's Drive with preference files.
Here's where our last feature comes in.
App folders are a special type of folder
where you can store files that only
your app can see and manage.
This folder still uses the user storage quota,
and they can clear all of the folder contents
if they want to stop using your app,
but the files won't ever get in their way.
So let's recap what we've achieved.
We took an app that stored of all of its content
on the local device and added automatic backups, cross device
file syncing, offline support, and synced app configuration.
You can add these features to your app too,
so get started with the Drive Android API
at developers.google.com/drive/android.
I'm Jen King, and I can't wait to see what you come up
with using the Drive Android API.