本文介绍了如何在Android应用程序上传文件,谷歌驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个功能,上传文件,谷歌驱动器。我的应用程序(Android应用程序)有两个页面。

I have implement a function upload file to Google Drive. My app (Android App) have two page.

设置页:我们已经配置了谷歌帐户。到谷歌帐户登录后。我们授予权和组oauth_token和oauth_token_secret保存到的共享preferences

Setting Page: we have config the google account. After login in to google account. We grant the right and save the OAUTH_TOKEN and OAUTH_TOKEN_SECRET to the SharedPreferences.

上传页:我们从资源管理器中选择文件。然后在按钮[UPLOAD]点击发送它们谷歌驱动器。

Upload page: We choose files from explorer. Then click on button [UPLOAD] to send them Google Drive.

我发现在developers.google.com样品code展示如何上传文件。它看起来像这样

I found the sample code on developers.google.com show how to upload file. It look like this

/**
   * Insert new file.
   *
   * @param service Drive API service instance.
   * @param title Title of the file to insert, including the extension.
   * @param description Description of the file to insert.
   * @param parentId Optional parent folder's ID.
   * @param mimeType MIME type of the file to insert.
   * @param filename Filename of the file to insert.
   * @return Inserted file metadata if successful, {@code null} otherwise.
   */
  private static File insertFile(Drive service, String title, String description,
      String parentId, String mimeType, String filename) {
    // File's metadata.
    File body = new File();
    body.setTitle(title);
    body.setDescription(description);
    body.setMimeType(mimeType);

    // Set the parent folder.
    if (parentId != null && parentId.length() > 0) {
      body.setParents(
          Arrays.asList(new File.ParentReference().setId(parentId));
    }

    // File's content.
    java.io.File fileContent = new java.io.File(filename);
    FileContent mediaContent = new FileContent(mimeType, fileContent);
    try {
      File file = service.files().insert(body, mediaContent).execute();

      // Uncomment the following line to print the File ID.
      // System.out.println("File ID: %s" + file.getId());

      return file;
    } catch (IOException e) {
      System.out.println("An error occured: " + e);
      return null;
    }
  }


所以我们只需要调用上述函数上传文件。但我不知道如何初始化
驱动器的(com.google.api.services.drive.Drive)的传递给函数。

我有什么事做的oauth_token和oauth_token_secret初始化驱动变量?

What thing i have to do to initialize the drive variable with OAUTH_TOKEN and OAUTH_TOKEN_SECRET ?

感谢你的援助。

推荐答案

在谷歌驱动器SDK文档中的OAuth 2.0页面介绍了如何检索有效的凭证,并用它们来实例化一个服务对象:

The OAuth 2.0 page in the Google Drive SDK documentation explains how to retrieve valid credentials and use them to instantiate a service object:

这篇关于如何在Android应用程序上传文件,谷歌驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 01:16