Android 打开和保存文件到/从 Google Drive SDK [英] Android Open and Save files to/from Google Drive SDK

查看:19
本文介绍了Android 打开和保存文件到/从 Google Drive SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去六个小时,我一直在翻阅来自 Google 的文档,但我仍然不知道如何开始.我要做的就是让我现有的 Android 应用程序可以从 Google Drive 读取文件,将新文件上传到 Google Drive,并编辑 Google Drive 上的现有文件.

I've spent the last six hours pouring over documents from Google and I still don't know how to get started with this. All I want to do is make it so my existing Android app can read files from Google Drive, upload new files to Google Drive, and edit existing files on Google Drive.

我读到 Drive SDK v2 只专注于让 Android(以及一般的移动设备)开发人员轻松使用它,但他们的文档中似乎几乎没有关于它的任何内容.

I've read that Drive SDK v2 was focused solely on making it easy for Android (and mobile in general) developers to use it, and yet there seems to be virtually nothing in their documentation about it.

理想情况下,我希望有人指出一些体面的文档、示例或教程,涵盖如何执行此操作(请记住,我使用的是 Android.他们有很多关于如何将云端硬盘与 Google 应用程序一起使用的资料引擎;我已经看过它,但我不知道如何从它转到 Android 应用程序.)

Ideally, I'd like someone to point at some decent documentation, example, or tutorial covering how to do this (keep in mind I'm using Android. They have plenty of stuff on how to use Drive with the Google App Engine; I have already looked at it and I have no idea how to go from that to an Android app.)

我需要知道我需要下载哪些库并将其添加到我的项目中,我需要添加到我的清单中,以及我最终如何从 Google Drive 中获取文件列表,下载一个,然后上传修改后的文件版本.

I need to know which libraries I need to download and add to my project, what I need to add to my manifest, and how I can ultimately get a list of files from Google Drive, download one, and then upload a modified version.

理想情况下,我希望它能够自动处理帐户,就像官方的 Google Drive 应用那样.

Ideally, I'd like it to handle accounts automatically, the way that the officially Google Drive app does.

推荐答案

Claudio Cherubino 说 Google Play 服务现已推出,这将使这个过程变得更加容易.但是,没有可用的示例代码(但是,他说它即将推出......他们说 Google Play 服务在 4 个月前即将推出",所以这个答案很有可能继续成为唯一完全有效的访问示例Google Drive 从您的 Android 应用程序到 2013 年.)

Claudio Cherubino says that Google Play Services is now available and will make this process a lot easier. However, there's no sample code available (yet, he says it's coming soon... they said Google Play Services was "coming soon" 4 months ago, so there's a good chance this answer will continue to be the only completely working example of accessing Google Drive from your Android application into 2013.)

Edit 2X:当我说谷歌要到明年才会有一个可行的例子时,我好像提前了一个月.来自 Google 的官方指南在这里:

Edit 2X: Looks like I was off by about a month when I said Google wouldn't have a working example until next year. The official guide from Google is over here:

https://developers.google.com/drive/quickstart-android

我还没有测试他们的方法,所以我从 2012 年 9 月(下)开始的解决方案可能仍然是最好的:

I haven't tested their methods yet, so it's possible that my solutions from September 2012 (below) are still the best:

此操作不需要 Google Play 服务.真是让人头疼,我花了 50 多个小时(100 多个小时)来弄清楚这一切,但这里有很多事情需要了解:

Google Play Services is NOT REQUIRED for this. It's a pain in the butt, and I spent well over 50 hours (edit: 100+ hours) figuring it all out, but here's a lot of things that it'll help to know:

图书馆

对于 Google 的一般在线服务,您的项目中需要这些库:(说明和下载链接)

For Google's online services in general you'll need these libraries in your project: (Instructions and Download Link)

  • google-api-client-1.11.0-beta.jar
  • google-api-client-android-1.11.0-beta.jar
  • google-http-client-1.11.0-beta.jar
  • google-http-client-android-1.11.0-beta.jar
  • google-http-client-jackson-1.11.0-beta.jar
  • google-oauth-client-1.11.0-beta.jar
  • guava-11.0.1.jar
  • jackson-core-asl-1.9.9.jar
  • jsr305-1.3.9.jar

特别是对于 Google Drive,您还需要这个:

For Google Drive in particular you'll also need this:

  • google-api-services-drive-v2-rev9-1.8.0-beta.jar (下载链接)

设置控制台

接下来,转到 Google 控制台.做一个新项目.在服务下,您需要打开两件事:DRIVE APIDRIVE SDK!它们是分开的,一个不会自动打开另一个,您必须同时打开!(弄清楚这一点浪费了我至少 20 个小时的时间.)

Next, go to Google Console. Make a new project. Under Services, you'll need to turn on two things: DRIVE API and DRIVE SDK! They are separate, one does not automatically turn the other on, and YOU MUST TURN BOTH ON! (Figuring this out wasted at least 20 hours of my time alone.)

仍然在控制台上,转到 API 访问.创建一个客户端,使其成为 Android 应用程序.给它你的包 ID.我认为指纹这件事实际上并不重要,因为我很确定我使用了错误的指纹,但无论如何都要尽量做到这一点(Google 提供了相关说明.)

Still on the console, go to API Access. Create a client, make it an Android app. Give it your bundle ID. I don't think the fingerprints thing is actually important, as I'm pretty sure I used the wrong one, but try to get that right anyways (Google provides instructions for it.)

它将生成一个客户 ID.你会需要那个.坚持住.

It'll generate a Client ID. You're going to need that. Hold onto it.

我被告知我弄错了,你只需要打开 Drive API,Drive SDK 根本不需要打开,你只需要使用 Simple API Key,而不是为 Android 设置一些东西.我现在正在研究这个问题,如果我弄清楚了,可能会在几分钟内编辑这个答案......

I've been told that I'm mistaken and that you only need to turn on Drive API, Drive SDK doesn't need to be turned on at all, and that you just need to use the Simple API Key, not set up something for Android. I'm looking into that right now and will probably edit this answer in a few minutes if i figure it out...

ANDROID 代码 - 设置和上传

首先,获取一个授权令牌:

First, get an auth token:

AccountManager am = AccountManager.get(activity);
am.getAuthToken(am.getAccounts())[0],
    "oauth2:" + DriveScopes.DRIVE,
    new Bundle(),
    true,
    new OnTokenAcquired(),
    null);

接下来,需要像这样设置 OnTokenAcquired():

Next, OnTokenAcquired() needs to be set up something like this:

private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> result) {
        try {
            final String token = result.getResult().getString(AccountManager.KEY_AUTHTOKEN);
            HttpTransport httpTransport = new NetHttpTransport();
            JacksonFactory jsonFactory = new JacksonFactory();
            Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
            b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
                @Override
                public void initialize(JSonHttpRequest request) throws IOException {
                    DriveRequest driveRequest = (DriveRequest) request;
                    driveRequest.setPrettyPrint(true);
                    driveRequest.setKey(CLIENT ID YOU GOT WHEN SETTING UP THE CONSOLE BEFORE YOU STARTED CODING)
                    driveRequest.setOauthToken(token);
                }
            });

            final Drive drive = b.build();

            final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
            body.setTitle("My Test File");
    body.setDescription("A Test File");
    body.setMimeType("text/plain");

            final FileContent mediaContent = new FileContent("text/plain", an ordinary java.io.File you'd like to upload. Make it using a FileWriter or something, that's really outside the scope of this answer.)
            new Thread(new Runnable() {
                public void run() {
                    try {
                        com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();
                        alreadyTriedAgain = false; // Global boolean to make sure you don't repeatedly try too many times when the server is down or your code is faulty... they'll block requests until the next day if you make 10 bad requests, I found.
                    } catch (IOException e) {
                        if (!alreadyTriedAgain) {
                            alreadyTriedAgain = true;
                            AccountManager am = AccountManager.get(activity);
                            am.invalidateAuthToken(am.getAccounts()[0].type, null); // Requires the permissions MANAGE_ACCOUNTS & USE_CREDENTIALS in the Manifest
                            am.getAuthToken (same as before...)
                        } else {
                            // Give up. Crash or log an error or whatever you want.
                        }
                    }
                }
            }).start();
            Intent launch = (Intent)result.getResult().get(AccountManager.KEY_INTENT);
            if (launch != null) {
                startActivityForResult(launch, 3025);
                return; // Not sure why... I wrote it here for some reason. Might not actually be necessary.
            }
        } catch (OperationCanceledException e) {
            // Handle it...
        } catch (AuthenticatorException e) {
            // Handle it...
        } catch (IOException e) {
            // Handle it...
        }
    }
}

安卓代码 - 下载

private java.io.File downloadGFileToJFolder(Drive drive, String token, File gFile, java.io.File jFolder) throws IOException {
    if (gFile.getDownloadUrl() != null && gFile.getDownloadUrl().length() > 0 ) {
        if (jFolder == null) {
            jFolder = Environment.getExternalStorageDirectory();
            jFolder.mkdirs();
        }
        try {

            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(gFile.getDownloadUrl());
            get.setHeader("Authorization", "Bearer " + token);
            HttpResponse response = client.execute(get);

            InputStream inputStream = response.getEntity().getContent();
            jFolder.mkdirs();
            java.io.File jFile = new java.io.File(jFolder.getAbsolutePath() + "/" + getGFileName(gFile)); // getGFileName() is my own method... it just grabs originalFilename if it exists or title if it doesn't.
            FileOutputStream fileStream = new FileOutputStream(jFile);
            byte buffer[] = new byte[1024];
            int length;
            while ((length=inputStream.read(buffer))>0) {
                fileStream.write(buffer, 0, length);
            }
            fileStream.close();
            inputStream.close();
            return jFile;
        } catch (IOException e) {        
            // Handle IOExceptions here...
            return null;
        }
    } else {
        // Handle the case where the file on Google Drive has no length here.
        return null;
    }
}

最后一件事...如果该意图被发送出去,您需要在它返回结果时进行处理.

One last thing... if that intent gets sent off, you'll need to handle when it returns with a result.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 3025) {
        switch (resultCode) {
            case RESULT_OK:
                AccountManager am = AccountManager.get(activity);
                am.getAuthToken(Same as the other two times... it should work this time though, because now the user is actually logged in.)
                break;
            case RESULT_CANCELED:
                // This probably means the user refused to log in. Explain to them why they need to log in.
                break;
            default:
                // This isn't expected... maybe just log whatever code was returned.
                break;
        }
    } else {
        // Your application has other intents that it fires off besides the one for Drive's log in if it ever reaches this spot. Handle it here however you'd like.
    }
}

安卓代码 - 更新

关于更新 Google Drive 上文件的最后修改日期的两个快速说明:

Two quick notes on updating the last modified date of a file on Google Drive:

  1. 您必须提供完全初始化的 DateTime.如果不这样做,您将收到来自 Google Drive 的错误请求"响应.
  2. 您必须对来自 Google 云端硬盘的文件使用 setModifiedDate(),并在更新请求本身上使用 setSetModifiedDate(true).(有趣的名字,嗯?setSet[...]",人们不可能打错那个...)

下面是一些简短的示例代码,展示了如何进行更新,包括更新文件时间:

Here's some brief sample code showing how to do an update, including updating the file time:

public void updateGFileFromJFile(Drive drive, File gFile, java.io.File jFile) throws IOException {
    FileContent gContent = new FileContent("text/csv", jFile);
    gFile.setModifiedDate(new DateTime(false, jFile.lastModified(), 0));
    gFile = drive.files().update(gFile.getId(), gFile, gContent).setSetModifiedDate(true).execute();
}

清单

您将需要以下权限:GET_ACCOUNTS、USE_CREDENTIALS、MANAGE_ACCOUNTS、INTERNET,并且很有可能您还需要 WRITE_EXTERNAL_STORAGE,具体取决于您希望将文件的本地副本存储在何处.

You'll need the following permissions: GET_ACCOUNTS, USE_CREDENTIALS, MANAGE_ACCOUNTS, INTERNET, and there's a good chance you'll need WRITE_EXTERNAL_STORAGE as well, depending on where you'd like to store the local copies of your files.

您的构建目标

右键单击您的项目,进入它的属性,如果需要,在 Android 下将构建目标更改为 Google API.如果它们不存在,请从 android 下载管理器下载它们.

Right click your project, go into it's properties, and under Android change the build target to Google APIs if you must. If they aren't there, download them from the android download manager.

如果您在模拟器上进行测试,请确保其目标是 Google API,而不是通用 Android.

If you're testing on an emulator, make sure its target is Google APIs, not generic Android.

您需要在测试设备上设置一个 Google 帐户.所编写的代码将自动使用它找到的第一个 Google 帐户(这就是 [0] 的含义).如果您需要下载 Google Drive 应用程序才能使其正常工作,请使用 IDK.我使用的是 API Level 15,我不知道这段代码还能运行多久.

You'll need a Google Account set up on your test device. The code as written will automatically use the first Google Account it finds (that's what the [0] is.) IDK if you need to have downloaded the Google Drive app for this to have worked. I was using API Level 15, I don't know how far back this code will work.

其余部分

以上内容应该可以帮助您入门,并希望您能从那里找到出路……老实说,这就是我到目前为止所获得的.我希望这可以帮助很多人,并为他们节省很多时间.我相当肯定我刚刚编写了最全面的设置指南来设置 Android 应用程序以使用 Google Drive.Google 感到羞耻,因为他们在至少 6 个不同的页面上传播了必要的材料,而这些页面根本没有相互链接.

The above should get you started and hopefully you can figure your way out from there... honestly, this is just about as far as I've gotten so far. I hope this helps A LOT of people and saves them A LOT of time. I'm fairly certain I've just written the most comprehensive set up guide to setting up an Android app to use Google Drive. Shame on Google for spreading the necessary material across at least 6 different pages that don't link to each other at all.

这篇关于Android 打开和保存文件到/从 Google Drive SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆