简体   繁体   English

如何使用Google Drive上的android上传/下载文件?

[英]How to upload/download files using android on google drive?

I am using Google drive rest API to perform upload/download operations on android, but there is no clear API documentation for resumable operations on the google doc. 我正在使用Google Drive Rest API在android上执行上载/下载操作,但是在google doc上没有可恢复操作的明确API文档。 I have successfully get the access token for drive operations. 我已经成功获取驱动器操作的访问令牌。 Even I cannot add google drive rest API tag. 即使我不能添加google drive rest API标签。 Any help would be appreciated. 任何帮助,将不胜感激。

I have successfully get the access token for drive operations. 我已经成功获取驱动器操作的访问令牌。 Any help would be appreciated. 任何帮助,将不胜感激。

        try {
            URL url;
            url = new URL("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable");
            HttpURLConnection request;
            request = (HttpURLConnection) url.openConnection();
            request.setRequestMethod("POST");
            request.setDoInput(true);
            request.setDoOutput(true);
            request.setRequestProperty("Authorization", "Bearer " + credential.getToken());
            request.setRequestProperty("X-Upload-Content-Type", getMimeType(file.getPath()));
            request.setRequestProperty("X-Upload-Content-Length", String.format(Locale.ENGLISH, "%d", file.length()));
            request.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            String body = "{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + "parentId12345678" + "\"]}";
            request.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length));
            OutputStream outputStream ;
            outputStream = request.getOutputStream();
            outputStream.write(body.getBytes());
            outputStream.close();
            request.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }


Reading the Google drive rest API doc setting the 阅读Google Drive Rest API文档设置

https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

will do the job 会做的工作

from the doc: 从文档:

The following example shows how to initiate a resumable session to 以下示例显示了如何启动一个可恢复会话来

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: image/jpeg
X-Upload-Content-Length: 2000000

{
  "name": "myObject"
}

暂无
暂无

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

相关问题 如何在Android中使用Google Drive API上传和下载? - How to upload and download using Google Drive API in Android? 如何使用Android API下载Google驱动器文件 - How to download google drive files using android api 如何使用Google Drive API上传文件? - How to upload files using the Google Drive API? 如何使用新的Google Drive REST API上传和下载文件? - How to upload and download files with the new Google drive REST API? 如何从 Google Drive 上传和下载文件(使用 Rest Api v3) - How to upload and download files from Google Drive (using Rest Api v3) 如何在 Android 工作室中以编程方式上传和下载 Google Drive 上的任何文件 - How to programmatically upload and download any file on Google Drive in Android studio 在 Android Studio 中使用 Google Drive API 从驱动器下载文件以上传到另一个驱动器 - Download file from drive to upload into another drive using Google Drive API in Android Studio 使用Google Drive Android API下载特定MIME类型的文件 - Download files of specific a MIME type using Google Drive Android API Android使用Google Play服务将文本文件从sdcard上传/下载到Google云端硬盘 - Android upload/download text files from sdcard to Google Drive with Google Play Services Google Drive Android API如何将音频文件上传到我的驱动器? 如何同步驱动器文件? - Google Drive Android API how to upload a audio file to my drive ? How to sync drive files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM