Google Drive SDK: Integrating your app with the Android Drive app
Articles Blog

Google Drive SDK: Integrating your app with the Android Drive app


CLAUDIO CHERUBINO:
Hi everyone. Welcome back to Google
Developers Live. My name is Claudio, and I’m one
of the engineers on the Google Drive developer
relations team. Today, I want to talk about how
to integrate your mobile app with the drive
app on Android. Why do that? Why should you do that? Sometimes, you want your
application to be able to open up a file that a user has, and
you don’t want to write the file browser yourself. Also, because the users already
have a file browser, which is the drive
app on Android. So you can piggyback on that and
make sure that whenever a user wants to open up a file
from there, and that file is supported by your app, you can
automatically land the user from the drive up
into your app. What I’m going to show you today
is very easy, and I’m not going to start
from scratch. We already showed you how we can
build your first drive app on Android, and we have a
quick start guide in our documentation. So I will point you to that. And if this is the first time
you build a drive app on Android, you might want to go
back to one of our recordings and watch the video where I
explain how to get started, how to start up the application,
how [? does ?] the API’s console project
and everything else. Once you’ve done that, you can
go back here and see how to improve the same quick start
application, so instead of doing whatever it was doing,
it will integrate with the drive app for Android. So I would just give you some
pointers, and if we take a look at my computer, what I have
here is the home page of the Drive SDK documentation,
which is available at developers.google.com/drive. We have a lot of things here. What you might want to
start with is the Android quick start. So if you go to the top right
corner in your page, you’ll see that we have five-minute
quick starts in multiple languages and multiple
platforms. One of them is the
Android one. So if you click on this icon
here, we will be brought to a page that is a step-by-step
guide that explains how to run your first drive
up on Android. There are some preliminary
steps, some configuration required, and you will have
to do all of this. And this, well– I mean, you will always have to
do these steps regardless of the type of application
you build in Android. So before we can do what
I’m doing today, you will have to do this. So the steps are just, very
quickly, you have to generate a sign-in certificate that’s
used to uniquely identify your app, and then you enable the
drive API into Google’s API console and we will see that. And then you create and
configure an Android project on your machine. I use Eclipse and with Eclipse,
you have a Google plug-in that helps you get the
libraries you need and get all the .jars you need. What I’m going to talk about
today is about integrating your drive app with Android,
and we have a specific page for that in our docs. And these page is titled,
Integrate with Android, so if you click on that, you will be
brought to another page that explains what I’m actually
doing today. So there are some
of the steps. There are some of the
configuration here that is explained and this is where
I’ll start from. So whenever you want to
integrate your Android application with Drive
application, the Google provided Drive application, the
first thing you have to do is to go to the APIs console. This is the API’s console
from my project. I created this the last time
I built my quick start. So there are some additional
steps here. First of those steps, is to go
to the API Access tab, and– Sorry, go to the Services
tab first, and enable the drive SDK. Last time we only enabled
the Drive API. With the Drive API,
you can read and write files from Drive. You can manage files from Drive,
but if you want to integrate with the UI, either
the mobile UI or the web UI, you also have to enable
the Drive SDK. So I did this, and once you do
that, you will have a new page in your API console page with
specific configuration for the Drive SDK, so you can go there,
you actually have to, and then all you have to do
here to integrate with a mobile app is to fill
the required fields. And the required feels are just
the application icon. I have an icon. So you can select your file. I used this cloud file. It’s just whatever
icon you want. That is required. And then, a document icon is
not actually required. The only other thing that is
required is the open URL. And the open URL, it is required
here, but it is not truly required in
a mobile app. So if you’re only building
a mobile app, you can put whatever you want like a valid
URL, but it doesn’t have to be an existing URL, so
I put example.com. We might want to change this
to streamline this process. If your app is only mobile, you
will not need to do this, so we might change this. But for the moment,
you will have to specify an open URL there. That’s it. You can save your changes. And your mobile application
now, will be ready to integrate with the Drive app. Let’s do this, I’m going to
show you what the app does live from my tablet, and
then, I will show the code that does that. So let’s switch to the tablet. And my hidden assistant will
help me with this. So if we switch to the tablet
and we click on the Drive application. This is the standard
Drive application. We have a list of files. In my application, you will see
how this is done, but my application can’t
open text files. So let’s say a GDL.txt file. Since my application is
registered to open those files, whenever you click on
that text file, you will have a choice of which application
you want to use to open this one. My application, I didn’t
even change its name. It’s called Drive Quickstart. So we can click on that. From this point, all that
is here is done by my application. So you do authentication, you
grant access to your app, and the app is very, very simple. The UI is really let’s
say, simple. What it does is basically, it
just opens the file and dumps the content of the file as
plain text on the screen. That said, now, if we go back
from here, we will go back to the Drive application. And if you have other files,
we can open other files. I said in my application that
it can also open HTML files. But it will not render them, it
will just open the HTML as plain text and dump
it into the page. And that said, this is what
my application does. Of course, you might want to
have other applications that, let’s say, open files like
videos, images, or render HTML, do whatever you want. Of course, here, your
application business logic is yours too, to make. Let’s see how this works. Let’s switch to Eclipse. There are basically two things
you have to do in Eclipse in your Android application. The first thing here, sorry,
this font might be a bit small, the code is going
to be bigger. So the first thing you have to
do is say, in the manifest, in your app manifest, that
your application can handle an intent. An intent called Drive Open. So whenever you click on a file
in the Drive application, what happens is that the Drive
application will fire intent, called Drive Open. Any application, any third party
application, that can handle that intent will be
listed for the user to choose. So my application, I added
this intent filter here. And when you define an intent
filter, which is basically how you say, I can handle this
intent, you say what is the name of the intent and enable
the intend is Drive Open. And then, well, this is a
default intent, and then, you have to specify for this intent,
you have to specify which MIME types your
application can open. My application can open plain
text files, can open HTML files, and can also open this
kind of weird MIME type. This MIME type is the MIME type
that is used to say, any file that this application
has created. This application will not create
any files, so this is probably useless in this
case, but you might want to have this. And the way you generate, the
way you construct, this MIME type, you always have
applications/vnd .googleapps.drive.sdk. And this number here
is your client ID. Where do you get the
app client ID from? It’s in the API’s console. So if I go back to the API’s
console, and I go back to my API access page, where I see
my credentials, where I generated my credentials, I see
that my client ID is that very number. So this uniquely identifies my
application, and this way I can say all files created but
this app will have this specific MIME type. If I go back to the Android
manifest, there is another thing to add here, which
is that you have to add your app ID here. Same thing, this is used to
uniquely identify your app, so I have app ID metadata with the
same value, which says ID equal to same thing. And then, I have to make sure
that my activity is supported. That’s it. That’s all you have to do to
configure your app and to support the intent. Now, of course, you have to
write the code that handles the intent. And this is the same way you
would handle any other intent, so let’s show that. And I was smart enough to make
the font bigger here. So whenever my application
starts, it starts as always, it starts on create. What I do is, I check if I have
an intent, and if I have an intent, I check what
the action is. This is how you should handle
it, because the user can go to your app directly and start it,
and you might want to do something there. In my case, this is very simple,
so I decided if the action is Drive Open, which is
defined in a string here, this constant string here, which
is the name of the intent I want to handle. So if the action is that,
I do something. Otherwise, I just kill my app. You might want to show
a landing page. You might want to show
a help page. You might want to
do something. You might want to tell the
user to go to Drive. You might do whatever it takes
in your app to help the users. But it is very simple, so I’ve
just killed the app. If the app is coming from the
intent, I take the file ID, the file ID, is what the Drive
app sends with the intent. So when you click on a file, the
file is not automatically send to the app. All that’s being sent is the
file ID, which is the unique ID for the file. Then, it is up to the
application to download the file content, get its metadata
and do anything else you want to do with the file. This makes sense, because,
remember, files in Drive can be very big, up to 5 gigabytes,
so we don’t want to transfer the file if
it’s not necessary. And that’s even more important
to mobile, so remember, you get the file ID, and if you
don’t need the content, just don’t download it. You might only want to change
the sharing permission, rename it, and all of these tasks do
not require you to download the file content, and
you will save 10 gigabytes of mobile data. So I take the file ID, I store
it in a variable called N file ID, and then, I start the
authorization part. So I have this other method
called Get User Account and Process File. What this does is– just here– is just starting
the auth flow. I won’t go into details
for this. We explained this, I hope, very
clearly, in the previous Google Developers
Live session. So what happens here, just
quickly, is that we use the Google Play services to
start the auth flow. Then, if you remember from the
demo we just did, the user will be prompted to choose an
account, and then grant access to the app. If the user has already granted
access to the app, you will only have to choose
an account. So all of this is done by
using the Google Play Services, which guarantees all
the security here and does all the things for you. So if you want to know more
about this, check the docs or check our video. So what I’m saying here is that
I want the user to have access to the Drive scope, which
is the full scope that is required to open all files,
and this case we could have asked for the Drive file
scope, which is a more restrictive scope, but I’m not
going into details here. And then we start an intent. This is how Google Play Services
works, so don’t worry about this. This is pretty standard. You started intent, which is
the choose account intent. You might remember that
we had first app was to choose an account. So the file is intent, the
dialogue pops up, the user selects an account, and then,
we go to an activity result, which is the callback method
that is called when the user has selected the account or the
user has or tries the app. We have two cases
we support here. One of them is when the user has
selected an account, the other one is when authorization
is required. Still no details here, but what
happens here is that we get credentials, and once we
have credentials, we can build the drive service. This is exactly the same code we
have in the Quickstart, so if you go to the Quickstart
page, you will see the exact same code. We built the drive service. Once we have a drive service
which is authorized by the user to read files, we call read
file from drive, and this is the new part. Read file for drive is here. I will share this code later. Read file from drive is here. I start a new thread. Remember, you cannot perform
any network requests on the main thread on Android, because
otherwise, the UI will freeze and the application
will die, and this will actually throw an exception. So you will have to spun new
thread or your async tasks to perform long running operations
somewhere else. And so what we’re doing here is
that we want to fetch the content of the file, and this
is done in two steps in the Drive API. First, you perform a get
using the file ID. The get will give you the file
metadata, which includes the title, which includes the
description, which includes the sharing permission,
and includes a link to the content. It’s called download URL. Once we have the metadata, then
we have a download URL, it will check if the download
URL is there, and if the download URL is actually valid,
if it is, we just send an HTTP authorize request to
it by building it here. And what we get back
is an input string. With an input string, there is
nothing that is Drive API specific here. These are just standard Java
Android programming. I get an input string. I can use it to build a string,
or in my case, I’m going to use a string builder,
so I have this helper method input string to string builder,
that I just wrote, which basically just reads
line-by-line the whole content of the file I passed,
so this string. So what I get here is just a
string builder, which contains all the lines from the
file I’m opening. And once I have the string
builder, I use it to show the text, which is just dump the
whole content of the file into the screen. Show [? the apps, ?] you might have better, fancier
solutions here. All I have in my app is
single text view. So I’m just taking the text
view, finding it by ID. I know the ID. This is hard coded,
just do what I do. And then, I set to text,
and that’s it. And the text will show up. Of course, you might want to
have the text be editable and maybe save it back to Drive. And you might want to build
something that is actually useful like this, but this is
how you take a file from Drive and land it into your app, open
it and get authorized, and do whatever you want
with the content. This is pretty easy and this
sample shows everything. Half of it is the same as the
previous example, so you will only have to check
the other half. And before I leave, I’ll
just go back to the documentation page. Remember, we have this Integrate
With the Android Drive App, which explains
basically the same thing. I’m going to add this
feed to this page. It explains the same thing, so
setting the MIME types in the app manifest, exporting the
manifest, handle the intent, what the intent name is, what
data you get from the intent, so everything else is here. And that’s basically it. So the code I used is– well, most of it comes from
this page so it should be pretty easy for you
to do the same. And with that, I think,
that’s it. Pretty sure this will be very
easy for you to follow. And if you have questions,
remember, you can always reach out to us for technical
questions on StackOverFlow or on Google+. It’s usually pretty easy to get
in touch with us, and we try to be as responsive
as possible. With that, thank you very much,
and I’ll see you next on Google Developers Live
on next Tuesday. Bye bye.

8 thoughts on “Google Drive SDK: Integrating your app with the Android Drive app

  1. The provided quickstart github project is missing all the gdrive specific tags, makes the github app unusable out of the box.

  2. I want to create an application which allows raspberry pi to upload files to google drive but each user have to download the credentials using an android app and use this credentials to upload to raspberry using ssh. Unfortunately, I got stuck.

  3. Who is in charge of the maintenance if this video? It has only 5 comments and no one answered… Which is the video where it is explained from scratch?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top