简体   繁体   English

从用户Google云端硬盘中选择任何文件,然后通过Android应用将其发送(上传)到我的服务器

[英]Pick any file from users Google Drive and send (Upload) it to my server via Android app

I am using the Google Drive Android API (GDAA) and I want to enable the user to pick any file (Google Docs, Sheets, Images, Video, Audio etc.) from their Drive and send it to the app server. 我正在使用Google Drive Android API(GDAA),并且希望使用户能够从其Drive中选择任何文件(Google文档,表格,图像,视频,音频等)并将其发送到应用服务器。

I have followed several tutorials on the web and also the official Google documentation for using the GDAA. 我已经在网上关注了一些教程以及有关使用GDAA的Google官方文档。

I tried the following answer as well: Fetching contents of Google Drive file through FileId 我也尝试了以下答案: 通过FileId获取Google云端硬盘文件的内容

But this too does not work. 但这也不起作用。 I get result.getStatus().isSuccess() as false always. 我总是将result.getStatus().isSuccess()设为false I do not understand why. 我不理解为什么。

The following is my open() function: 以下是我的open()函数:

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

And the following is the callback where I get result.getStatus().isSuccess() as false : 以下是我得到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);
            }
        };

For now I am relying on this code. 现在,我依靠此代码。 I have been able to open Google Sheets and Docs but I need to upload them as well as images and videos to my app server (Opening the file is not necessary). 我已经能够打开Goog​​le表格和文档,但是我需要将它们以及图像和视频上传到我的应用服务器(无需打开文件)。 Is there a way I can achieve this? 有没有办法可以做到这一点?

I have read about the Storage Access Framework (SAF) and it may allow me to access the Google Drive files Kitkat onward but my app supports devices running on JellyBean and upwards and thus I am going with the GGDA. 我已经阅读了有关Storage Access Framework(SAF)的信息,它可能允许我从以后访问Kitkat的Google云端硬盘文件,但是我的应用支持在JellyBean以及更高版本上运行的设备,因此我要使用GGDA。

I have not checked the REST API yet. 我尚未检查REST API。 I want to understand if I am doing this the right way or is there a totally different way to achieve what I want. 我想了解我是否以正确的方式进行操作,或者是否有完全不同的方式来实现自己想要的目标。 And if this is the right way, what is going wrong? 如果这是正确的方法,那么哪里出了问题?

Any help will be appreciated. 任何帮助将不胜感激。

You may try the following workaround: 您可以尝试以下解决方法:

  • Make sure that you generate the correct configuration with your correct sha1. 确保使用正确的sha1 生成正确的配置
  • Make sure that the google-services.json in your app folder is correct. 确保您的应用文件夹中的google-services.json是正确的。
  • Try to go to Credentials page and make sure that you choose the correct project. 尝试转到“ 凭据”页面 ,并确保选择正确的项目。 It should be same as the one you used to generate configuration files. 它应该与用于生成配置文件的文件相同。
  • In the Credentials section , make sure that your OAuth 2.0 client IDs type is Web Application not Android. 在“ 凭据”部分中 ,确保您的OAuth 2.0客户端ID类型为Web应用程序而不是Android。
  • Make sure that your API key in your string.xml matches the one in your Google Developer Console. 确保string.xml中的API密钥与Google Developer Console中的API密钥相匹配。

If these don't help, try to create a new API key based on a new project in Google Developer Console. 如果这些方法没有帮助,请尝试在Google Developer Console中基于新项目创建新的API密钥。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件从Android应用上传到Google驱动器 - Upload file from android app to google drive 从Android应用上传Google Drive Sqlite数据库文件 - Google Drive Sqlite db file upload from android app 如何通过电子邮件发送保存的 CSV 文件或在 Android 中使用 Google Drive 上传? - How to send saved CSV file via email or upload with Google Drive in Android? 无法使用教程将文件上传到Google云端硬盘(Android上为Drive App) - Unable To Upload File to Google Drive Using Tutorial (Drive App on Android) 通过文件扩展名在我的 Android 应用程序中打开来自 Google Drive 的文件 - Opening files from Google Drive in my Android app by file extension 无法从我的Android应用com.google.android.gms.auth.GoogleAuthException上传到Google驱动器:未知 - cannot upload to google drive from my android app com.google.android.gms.auth.GoogleAuthException: Unknown 如何在 Android 工作室中以编程方式上传和下载 Google Drive 上的任何文件 - How to programmatically upload and download any file on Google Drive in Android studio 将Google云端硬盘实施到Android应用并从Google云端硬盘下载文件 - Google Drive Implementation to Android app and Downloading the file from Google drive 如何将图像从我的Android应用程序上传到Google驱动器上的特定文件夹 - how to upload an image from my android app to a specific folder on google drive 如何在Android App中创建上传文件到Google云端硬盘 - how to create upload file to Google Drive in Android App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM