【问题标题】:Pick any file from users Google Drive and send (Upload) it to my server via Android app从用户 Google Drive 中选择任何文件并通过 Android 应用程序将其发送(上传)到我的服务器
【发布时间】:2016-10-13 13:47:13
【问题描述】:

我正在使用 Google Drive Android API (GDAA),我希望用户能够从他们的 Drive 中选择任何文件(Google 文档、表格、图像、视频、音频等)并将其发送到应用服务器。

我遵循了网络上的几个教程以及使用 GDAA 的 Google 官方文档。

我也尝试了以下答案:Fetching contents of Google Drive file through FileId

但这也行不通。我总是把result.getStatus().isSuccess() 当作false。 我不明白为什么。

以下是我的open()函数:

private void open() {
    DriveFile driveFile = mFileId.asDriveFile();
    driveFile.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
            .setResultCallback(driveContentsCallback);
    mFileId = null;
}

以下是我将result.getStatus().isSuccess() 设为false 的回调:

/**
 * This is Result result handler of Drive contents.
 */
final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {

                if (!result.getStatus().isSuccess()) {
                    return;
                }

                // Read from the input stream an print to LOGCAT
                DriveContents driveContents = result.getDriveContents();
                BufferedReader reader = new BufferedReader(new InputStreamReader(driveContents.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String contentsAsString = builder.toString();
                Log.i("ContentAsString", contentsAsString);

                // Close file contents
                driveContents.discard(mGoogleApiClient);
            }
        };

现在我依赖于这段代码。我已经能够打开 Google 表格和文档,但我需要将它们以及图像和视频上传到我的应用服务器(不需要打开文件)。有什么方法可以实现吗?

我已阅读有关存储访问框架 (SAF) 的信息,它可能允许我继续访问 Google Drive 文件 Kitkat,但我的应用支持在 JellyBean 及更高版本上运行的设备,因此我将使用 GGDA。

我还没有检查 REST API。我想了解我是否以正确的方式这样做,或者是否有完全不同的方式来实现我想要的。如果这是正确的方法,那么出了什么问题?

我们将不胜感激。

【问题讨论】:

    标签: android google-drive-api google-drive-android-api


    【解决方案1】:

    您可以尝试以下解决方法:

    如果这些都没有帮助,请尝试在 Google Developer Console 中基于新项目创建新的 API 密钥。

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多