Placeholder Image

字幕表 動画を再生する

  • [MUSIC PLAYS]

  • CLAUDIO CHERUBINO: Hi everyone.

  • Welcome back to Google Developers Live.

  • My name is Claudio and I'm one of the engineers in the Google

  • Drive Developer relations team.

  • We get a lot of requests from you about what you want us to

  • talk about in Google Developers Live, but there is

  • one topic that most of you ask for, and that is how do I

  • write an Android app that uses Google Drive.

  • This is a very common feature request, and this is a very

  • important scenario for us.

  • So today I'm going to show you how you can write your first

  • Android application that uses Drive.

  • We also added this tutorial to the documentation on

  • developers.google.com/drive just today.

  • Probably two hours ago.

  • So you can watch this video, and if you need some guidance,

  • you missed some commands, you can always go back to our

  • documentation or you can watch this video again.

  • It's going to be recorded and you can watch it at any time.

  • So if we go to our documentation, as I said, in

  • the home page we have the list of five minute quick starts in

  • different languages.

  • The last one we added is Android.

  • We recently added Objective C. We're going to talk about it

  • in a future session of Google Developers Live.

  • But today we're going to talk about Android.

  • So if we click on this Android link here we will be brought

  • to the Android specific page.

  • It looks like all the other quickstarts.

  • So you might have seen this quickstart already.

  • We covered most of them on Google Developers Live.

  • The goal with these quickstarts is to have an

  • application in a few minutes, an application that integrates

  • with Drive.

  • As usual, being a quickstart, we tend to cut some corners.

  • So this is not going to be a perfect application.

  • We might not follow Android best practices today.

  • But still, with by following this you will have something

  • running and then you could always go back to it and make

  • it better, make it a production application.

  • If we look at our documentation there are five

  • steps to have this application running.

  • And the first one is very specific to Android.

  • So the first step is generating the signing

  • certificate fingerprint.

  • It's only required when writing an Android app.

  • Android has a unique authorization mechanism.

  • So it still uses Auth2, like we do with all other languages

  • in all other platforms.

  • But it requires the developers to register their package name

  • in signing certificates in the API's console.

  • So this is what we need to do as our first step.

  • If you're an Android developer you've

  • probably seen this already.

  • Whenever you want to publish an application on

  • play.google.com you have to sign your certificate with

  • your product.

  • This is the same thing and this is what we're going to do

  • and let me show how this works.

  • So the first thing to do is to generate the SHA1 fingerprint.

  • And you can do it by using a command

  • line tool called keytool.

  • There is the command line here in the Docs.

  • So if you're following instructions, as I'm doing,

  • you can just select it, and copy it, and execute it in

  • your shell.

  • So let me move to my shell.

  • I paste the command.

  • I'm using keytool to export my certificate

  • from the debug keystore.

  • This is important.

  • Every time you write an application in debug mode in

  • Eclipse it automatically generates a debug

  • keystore for you.

  • So we're going to use that debug keystore for our debug

  • application.

  • Whenever you want to move your application from debug to

  • production, you have to generate your own keys.

  • And this is well documented in the Android documentation.

  • If you're using the debug keystore, its default location

  • is inside a hidden folder in your home directory and the

  • file is going to be called debug.keystore.

  • When we ask keytool to export a certificate, we are asked to

  • type the keystore password.

  • If you're using the debug keystore password that is

  • automatically generated by Eclipse, the default password

  • is Android.

  • So I'm going to type Android and what I get is some

  • information about my key.

  • And what I need is the SHA1 fingerprint This line here.

  • We're going to use it in the second step.

  • So we're done with the first step.

  • So the first step is to recap exporting your signing

  • certificate SHA1.

  • And this is done using keytool.

  • Nothing special if you know about Android development.

  • So let's go back to the docs.

  • So we're done with the first one.

  • Of all those information what you need is the SHA1

  • certificate, the SHA1 fingerprint.

  • So we're going to keep him handy for later.

  • Second step is enabling the drive API

  • in the API's console.

  • This is the same thing you would do with any other

  • language in any other platform.

  • So we go to the API's console and we create a new project.

  • I'm going to click on create project.

  • My session is expired.

  • I'm going to reload the page and create a new project, and

  • then I have to enable the APIs I need.

  • To Drive API application I need to enable the drive API.

  • So I just click on Drive API, click activate, and that's it.

  • The drive API is now enabled.

  • Now I need to generate my credentials.

  • So to do that, I go to the API axis tab and

  • click on create [? Auth ?]

  • 2.0 client ID.

  • When I do, I have a step by step wizard

  • that I have to follow.

  • In the first page I have to specify my product name, my

  • application name.

  • Let's say it's going to be Drive Quickstart.

  • You can also add a product logo and a homepage

  • URL if you have it.

  • If you don't just move on and click Next.

  • In the second page we have to select installed application

  • as the application type.

  • You can select different types of application.

  • One is a web application.

  • Otherwise a service account application we also covered

  • those in another session to Google Developers Live but

  • what we want is an installed application, which is an

  • application that is installed on the device.

  • So when I click on it I then have to choose whether I want

  • an Android app, IUS app, or any other kind of installed

  • application.

  • We're building an Android app so this

  • is going to be Android.

  • And when we do we have to specify the package name in

  • the signing certificate fingerprint.

  • This pair has to be unique and these uniquely identifies your

  • application, so that whenever an API call comes from an

  • application, Google will check if it matches this package

  • name and signing certificate.

  • So we have to be extra careful here.

  • We cannot use a package name and fingerprint pair we

  • already used.

  • So in our example the package name is going to be

  • com.example.drivequicksearch.

  • If you use these values you can basically copy and paste

  • the rest of the code from the documentation.

  • If you change this, obviously, you will have to change the

  • package name in your application.

  • The easiest way to get started with this quickstart, is to do

  • exactly as we do, which is even using the

  • same package names.

  • And obviously my package name will be the same as yours

  • because we will all use com.example.drivequickstart

  • but my signing certificate fingerprint, the SHA1 string

  • we generated, will be unique.

  • So the pair, package name, and SHA1 will be unique.

  • So we need the SHA1 and we can get it back from the terminal.

  • So let's go back here.

  • We need this SHA1 string we just generated.

  • We copy it from the terminal and we

  • paste it in the dialogue.

  • That's all it takes.

  • We can create client ID.

  • Now you can see in the API's console that we have our

  • credentials.

  • So we have a client ID.

  • We have an application type Android.

  • We have our package name specified and our certificate

  • fingerprint.

  • Since this is the debug certificate that Eclipse

  • already uses, when I will build my application using

  • Eclipse it will be automatically signed with this

  • same key, assuming that I'm running it in debug mode.

  • So if we go back to the dock, this is explained and we are

  • basically done with step two.

  • Step three is in Eclipse.

  • So we're going to create and configure an Android project

  • that uses the Drive API.

  • So I have Eclipse open here.

  • I have no other projects, so I'm going

  • to start from scratch.

  • I'm going to do file, new project, and select Android

  • application project.

  • Here I have to specify my application name.

  • In order for it to match what we put in the API's console

  • the application name has to be drivequickstart.

  • Remember if you change the application name to be your

  • own, you have to match it in the API's console.

  • So I'm going to type drivequickstart.

  • And as you can see the package name is automatically set to

  • com.example.drivequickstart, which is what I have in the

  • API's console.

  • What I'm going to do next is to set the latest version of

  • the target SDK.

  • So we now have devices with the latest version of the SDK.

  • The Android SDK is 4.2.

  • So I'm going to have target SDK API 17, Android 4.2 just

  • to get the latest.

  • And we're going to compile with same version but with

  • Google APIs.

  • Everything else in this dialogue, in this wizard, we

  • can leave it as it is.

  • So I'm going to click next.

  • The next part is about some other

  • configuration in the project.

  • You don't need to change anything here if you want to

  • follow the quickstart.

  • Of course, when you're writing your application you will have

  • to change things here maybe.

  • You can set the icon.

  • We're going to use the default one.

  • It doesn't matter.

  • Then when you have a production app you can change

  • the icon and all the other things.

  • And we're going to create a blind activity.

  • Remember everything in Android starts from an activity.

  • And this activity is going to be called with a default name

  • which is main activity.

  • Its fine with me.

  • Remember, whenever you want to write your own production

  • application you can change it.

  • You probably have to change these values.

  • So I click finish and the basic skeleton of my

  • application is created.

  • And this is how the application would look like,

  • but we will see later.

  • We're not going to show this page a lot.

  • So what this is the application going to do?

  • This application is going to fire up the camera, the device

  • camera, allow users to take photos, and whenever a photo

  • is taken it will be automatically uploaded to

  • Google Drive.

  • And that's very easy.

  • So let me show you how to do it.

  • So the next thing we have to do is to add the drive API to

  • the project.

  • We have a Java client library that can be used.

  • And the easiest way to add all the required .jars is to use

  • the Google plug-in for Eclipse.

  • So this is one of the prerequisites for this

  • quickstart.

  • If you install the Google plug-in for Eclipse, then you

  • have a button in your toolbar.

  • And one of the commands available here

  • is add Google APIs.

  • So if I click on that I see the list of all APIs I can

  • add, just with a click.

  • So I'm going to look for Drive API v2.

  • Remember, we have version one and version two.

  • Version two is the newest with a lot more features, but v1 is

  • also supported.

  • But I mean if you're building any application you

  • should use Drive v2.

  • So I click on Drive v2, click finish, and the plug-in is

  • going to download the latest version of the Drive API

  • client library together with the latest version of the Java

  • client library in all required dependencies.

  • And add them to the project.

  • So it's very easy.

  • Not a lot of steps here.

  • It just adds everything you need for the

  • application to build.

  • The only other .jar you need is google-play-services.jar We

  • recently launched this google-play-services.jar.

  • It is the new way for developers to perform

  • authorization in Android applications.

  • All I'm going to show you here, uses this

  • google-play-services.jar and what we did in the API's

  • console requires the google-play-services.jar.

  • So we need to add the .jar.

  • You have to download the .jar in your local computer under

  • the SDK manager and then you can add

  • it to the Java project.

  • I already downloaded it.

  • Click on Drive quickstart.

  • I can do project, properties, go to the library's tab and do

  • add external .jars.

  • I should browse for my google-play-services.jar on my

  • local computer and add it.

  • Then I have to go to order and export, to check the box next

  • to it and make sure the .jar is exported when we build the

  • application.

  • So we added all the .jars, now we need to add the code, the

  • code you're going to use and I'm going to show it to you.

  • I'm going to show the app and then I'm going to explain what

  • the app does step by step.

  • The code is all available in a single file in the

  • documentation.

  • This is probably one of the non-best practices we're

  • following, which is to put everything in a single file.

  • Obviously, in a production application you might want to

  • split the activities in the files in separate files.

  • But here it is just easier for you to copy and

  • paste all of this code.

  • It's not that long.

  • It's probably 100 lines of code.

  • And take all this code and replace the one that we have

  • in Eclipse.

  • So I'm going to go to source, my namespace, the

  • mainactivity.java.

  • I'll take all of this, delete this and put my code.

  • If you used com.example.drivequickstart as

  • your namespace in your package name this is

  • going to build correctly.

  • Otherwise it would have to adopt the code to match it.

  • The only other thing we have to do is to add a couple of

  • permission to the Android manifest.

  • So in the Android manifest you specify what permissions your

  • application requests.

  • And this application requires two permissions.

  • The get account permission, which is for the application

  • to fire up the get account intent and let the user choose

  • an account to use.

  • And the other permission we need is internet.

  • Well, for the application to be able to send HTTP requests.

  • So this is also covered in the documentation.

  • We have this other box here.

  • So we need to take these two lines, and there are multiple

  • ways to add permissions to your Android app, but we need

  • to take these two lines and put them in

  • androidmanifest.xml.

  • You just have to put it anywhere inside of manifest.

  • That's it.

  • The application is ready to run.

  • I already have Android virtual machine running, which is

  • here, and it's a phone device, an emulated phone device.

  • And so what I'm going to do is click on debug to run the

  • application.

  • The application will be installed on my device, and

  • we'll see what happens in the device.

  • So the application is now building and then being

  • installed on the phone.

  • And then it will be automatically run.

  • So what this application does is, first, it will ask the

  • user for an account to use.

  • This dialogue is fired by an intent.

  • It's a standard Android intent, and it lets the user

  • pick an account to use.

  • We want to publish photos on someone's Google Drive, so we

  • need to select which Google Drive account we want to use.

  • And this will list all the accounts I have on my phone.

  • I only have one but you can also add accounts.

  • It's a good practice to only show this the first time the

  • application runs and then remember this setting.

  • So the next time the same user starts the application, you

  • don't have to ask it again.

  • We're not doing this in the application in

  • order to keep it simple.

  • So I'm going to select my account.

  • What happens next is that the application

  • will fire up the camera.

  • If you've used the emulator before you know that this is

  • how do the emulated camera looks like.

  • We have this moving background and we have

  • a box moving around.

  • This is a camera.

  • Of course, if you use your physical device you will lose

  • your real camera.

  • But this is good enough for us to see what's going on.

  • I'm going to take a photo, beautiful, and confirm.

  • Now the application will try to upload the file, the photo

  • we just took, to Drive.

  • The problem is the app was never approved by the user, so

  • the application realizes that we need

  • approval from the user.

  • And that's why this dialogue is shown.

  • So this is a standard dialogue.

  • Standard [? Auth2 ?]

  • dialogue.

  • It says, do you want DriveQuickstart, which is the

  • name of the application I just created, do you want it to be

  • able to view and manage the files and documents I have on

  • Google Drive?

  • Yes, I do.

  • This confirmation will only be asked once, so now we will

  • remember it.

  • And in the future every time you use the same application

  • we will not ask for [? DSAK. ?]

  • What's going on now is that the photo was just uploaded.

  • I know the UI's not great.

  • What you should expect is that maybe instead of a blank page

  • you show a progress dialogue, things like that, but that's

  • an improvement we can do to the application.

  • Now we can take photos, confirm, and it will just send

  • them to Drive.

  • The only thing that happens it will show a small toast, which

  • is a small message saying that the photo was uploaded, and

  • then get back to the camera.

  • See, the photo was uploaded.

  • We can leave it running, but if I go back to my Drive

  • account, this is the Drive account for the same user, I

  • should be able to see the photos I just took.

  • Or not.

  • They should be here.

  • Anyway they probably show up.

  • So let me explain this code and let's see how it works.

  • Let's go back to the code.

  • This is all you all you need to run this application.

  • As you know everything starts in the own create method of

  • the activity.

  • So in own create the first thing we do is to instant

  • shade our credential using [? Auth2 ?]

  • And we're saying that we want to use the Drive scopes, which

  • is the full scope that allows the application to do

  • basically everything on my Google Drive file.

  • After that we start an activity.

  • So we fire intent, which is the new choose account intent.

  • Which is as you remember the first thing we the application

  • did was asking us for which account we wanted to use.

  • So this intent will be uniquely identified by this

  • constant, which is called request account picker.

  • As you know, when we get back something from an intent, we

  • have to handle it in a method called onActivityResult.

  • So in onActivityResult we check the request code.

  • First case what we want is if we are in

  • request account picker.

  • So the user itself.

  • I just chose an account.

  • If we are in this case and the result is OK, which means the

  • user has actually picked an account and we have data, the

  • data we need is the account name, which is the email

  • address of the user that was chosen by the user.

  • With that we can set the account name in the credential

  • and then we can use these credentials to

  • get the Drive service.

  • So let me show you get drive service and see what it is.

  • Get drive service is here at the bottom.

  • We use the builder pattern to build a drive service object,

  • which is the one that's going to be used to

  • read and write files.

  • And what we have to do with it?

  • We just have to pass some parameters including

  • transport, JSON factory and our credential.

  • And then we build it and that's it.

  • So we have drive service with our credential, and if we go

  • back to the activity, what we do next is

  • starting the camera.

  • Let's see what start camera intent does.

  • Start camera intent does two things.

  • First, it generates a file name for the photo

  • we're going to take.

  • And the file name is simply image_timestamp.jpg, so each

  • photo is uniquely identified.

  • And once we decided what the file name will be, we fire

  • intent, which is a camera intent to capture an image.

  • And that's why after the user has picked an account we go

  • directly to the camera.

  • This is still an intent, so the result of the intent will

  • be handled by onActivityResult.

  • And onActivityResult has a case for capture image.

  • When we capture an image and the result is OK, we call save

  • file to drive, which is where the Drive API calls her a

  • [? mate. ?]

  • So if we go to save file to drive, there's a very

  • important thing here to notice.

  • We are spawning a threat.

  • Why do we do it?

  • Because remember in Android you cannot send network

  • requests on the main thread, because if you do, DUI will

  • freeze and it will throw an exception.

  • So whenever you want to do network requests you have to

  • use a thread, or an an [? async ?] task.

  • I decided to use a thread, basically the same.

  • An [? async ?] task is basically a wrapper around the

  • thread and some other helper method.

  • But what this thread does is reading the file that we saved

  • from the phone.

  • So when the camera takes a photo, it saves it in a

  • specific location that we define.

  • So this intent will read the file from there and send it to

  • drive using this call, service.files.insert.

  • So the service object, which is the drive service object,

  • exposes the number of methods in a number of resources.

  • One of them is files.

  • And on files we have different methods including insert,

  • update, list, get, delete, and so on.

  • So we call it, we execute this insert request, and then we

  • get back to file, which is the metadata we

  • got back from Drive.

  • When that happens or when we get the file back--

  • we showed a toast, which is the small message we saw,

  • which says photo uploaded in the file title, and we saw the

  • camera again.

  • So that the user can go back and take new photos.

  • There is an important catch clause here.

  • So all of this was wrapped in a try catch clause.

  • Why?

  • Because whenever we send the drive service insert request

  • it might happen that the application is not authorized

  • by the user.

  • Remember, the first time I took a photo I had to grant

  • access to the app.

  • How is that handled?

  • So when the insert request is sent, if the application is

  • not authorized, it will throw a user recoverable [? Auth ?]

  • I/O exception that you have to catch.

  • When you catch that exception, you simply have to fire the

  • intent that is in the exception and, we call it,

  • request authorization.

  • So this is what happened.

  • I sent a request.

  • The application wasn't authorized.

  • So the first time I did it, I went into the catch clause.

  • I started an activity asking the user for permission.

  • The activity is handled like all other activities in

  • onActivityResult.

  • And we have the two cases as usual.

  • Good case the user said yes and then we saved the file.

  • So we called save file to drive again and that's it.

  • And this time it's going to work.

  • If the user said, no, then we go back to

  • choosing the account.

  • So maybe the user decided to use a different account, and

  • doesn't want the app to be authorized for that account.

  • So if that fails, you have to send back the user to the

  • initial page and say, OK, which account

  • do you want to use?

  • And this is what we're doing.

  • In our case I said, yes.

  • And I called save file to drive again.

  • It did the same thing, the request went through, and the

  • file was created.

  • The second time and all the other times I use the

  • application, it will just go there instead of

  • throwing the exception.

  • And that's basically it.

  • The only other method I have here is show toast, which is

  • just a helper method to show that message.

  • But you can use any kind of notification you want.

  • You can even write to logs and so on.

  • And that's basically it.

  • It took probably more than 10 minutes, but if you do not

  • have to talk through it, and you just follow the

  • instructions, it's probably very, very fast.

  • And as I said at the beginning this tutorial is also

  • available on developers.google.com/drive.

  • You will see a link on the home page.

  • We think this is going to help a lot of Android developers

  • that want to integrate with Drive.

  • We had some reports that things were not as smooth as

  • we thought.

  • We think they're now much better.

  • But if you have any questions, we'll be really glad to answer

  • your questions.

  • Please feel free to get in touch with us on SecOverflow

  • on Google+.

  • And if you have Android applications that use Drive,

  • please let us know.

  • We are really curious about your integration.

  • And I think that's it.

  • I want to thank you all for your time.

  • And see you next time on Google Developers Live.

  • Bye bye.

  • [MUSIC PLAYS]

[MUSIC PLAYS]

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

Google Drive SDK。Androidで初めてのDriveアプリを書く (Google Drive SDK: Writing your first Drive app on Android)

  • 169 10
    羅致 に公開 2021 年 01 月 14 日
動画の中の単語