gpt4 book ai didi

Android 在 Google Drive SDK 中打开和保存文件

转载 作者:IT老高 更新时间:2023-10-28 13:25:14 27 4
gpt4 key购买 nike

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

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

理想情况下,我希望有人指出一些有关如何执行此操作的体面文档、示例或教程(请记住,我使用的是 Android。他们有很多关于如何将 Drive 与 Google App Engine 配合使用的内容;我已经看过了,我不知道如何从它转到 Android 应用程序。)

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

理想情况下,我希望它能像正式的 Google Drive 应用程序那样自动处理帐户。

最佳答案

编辑:克劳迪奥·凯鲁比诺 (Claudio Cherubino) 说 Google Play 服务现已可用,这将使此过程变得更加容易。但是,没有可用的示例代码(但是,他说它即将推出......他们说 Google Play 服务在 4 个月前“即将推出”,所以这个答案很有可能将继续成为唯一完全有效的访问示例谷歌驱动器从你的 Android 应用程序到 2013 年。)

编辑 2X:当我说谷歌要到明年才会有工作示例时,我似乎已经离开了大约一个月。 Google 的官方指南在这里:

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

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

为此不需要 Google Play 服务。这很痛苦,我花了 50 多个小时(编辑:100 多个小时)才搞清楚这一切,但了解以下很多事情会有所帮助:

图书馆

对于 Google 的一般在线服务,您的项目中需要这些库:( 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,您还需要这个:
  • google-api-services-drive-v2-rev9-1.8.0-beta.jar ( Download Link )

  • 设置控制台

    接下来,去 Google Console .做一个新项目。在服务下,您需要打开两件事: 驱动API 驱动 SDK !它们是分开的,一个不会自动打开另一个,并且 你必须同时打开! (弄清楚这一点浪费了我至少 20 个小时的独处时间。)

    仍然在控制台上,转到 API Access。创建一个客户端,使其成为 Android 应用程序。给它你的包 ID。我认为指纹实际上并不重要,因为我很确定我使用了错误的指纹,但无论如何都要尝试正确处理(Google 提供了相关说明。)

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

    编辑:有人告诉我我错了,你只需要打开 Drive API,Drive SDK 根本不需要打开,你只需要使用简单的 API key ,而不是设置为 Android 准备一些东西。我现在正在研究这个问题,如果我弄清楚了,可能会在几分钟内编辑这个答案......

    ANDROID 代码 - 设置和上传

    首先,获取一个身份验证 token :
    AccountManager am = AccountManager.get(activity);
    am.getAuthToken(am.getAccounts())[0],
    "oauth2:" + DriveScopes.DRIVE,
    new Bundle(),
    true,
    new OnTokenAcquired(),
    null);

    接下来, OnTokenAcquired() 需要设置如下:
    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...
    }
    }
    }

    ANDROID代码-下载
    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;
    }
    }

    最后一件事......如果该 Intent 被发送,您将需要在它返回结果时进行处理。
    @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.
    }
    }

    ANDROID 代码 - 更新

    关于更新 Google Drive 上文件的最后修改日期的两个快速说明:
  • 您必须提供一个完全初始化的 DateTime。如果不这样做,您将收到来自 Google 云端硬盘的“错误请求”响应。
  • 您必须在 Google Drive 的文件上使用 setModifiedDate() 并在更新请求本身上使用 setSetModifiedDate(true) 。 (有趣的名字,嗯?“setSet[...]”,人们不可能打错那个...)

  • 下面是一些简短的示例代码,展示了如何进行更新,包括更新文件时间:
    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,具体取决于您想要存储文件的本地副本的位置。

    您的构建目标

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

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

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

    其余

    以上应该让你开始,希望你能从那里找到出路......老实说,这只是我到目前为止所得到的。我希望这可以帮助很多人并为他们节省很多时间。我很确定我刚刚编写了最全面的设置指南来设置 Android 应用程序以使用 Google Drive。谷歌将必要的 Material 传播到至少 6 个完全没有相互链接的不同页面,这让他们感到羞耻。

    关于Android 在 Google Drive SDK 中打开和保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164024/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com