android - 如何使用 Google Drive API 上传文件?

标签 android google-drive-android-api

我正在尝试使用 Android 上的 Google 云端硬盘 api 上传文件

https://github.com/googlesamples/google-services/tree/master/android/signin/app/src/main/java/com/google/samples/quickstart/signin

我在上面的链接中注册了 SignInActivityWithDrive.java。

但是没有上传文件、下载文件的例子

我想知道如何上传和下载文件

谢谢

最佳答案

您可以找到 uploading 的基本示例和 downloading在文档中。

正在上传

You can send upload requests in any of the following ways:

  • Simple upload: uploadType=media. For quick transfer of a small file (5 MB or less). To perform a simple upload, refer to Performing a Simple Upload.
  • Multipart upload: uploadType=multipart. For quick transfer of a small file (5 MB or less) and metadata describing the file, all in a single request. To perform a multipart upload, refer to Performing a Multipart Upload.
  • Resumable upload: uploadType=resumable. For more reliable transfer, especially important with large files. Resumable uploads are a good choice for most applications, since they also work for small files at the cost of one additional HTTP request per upload. To perform a resumable upload, refer to Performing a Resumable Upload.

以下示例演示如何使用客户端库上传图像:

File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id")
    .execute();
System.out.println("File ID: " + file.getId());

正在下载

Depending on the type of download you'd like to perform — a file, a Google Document, or a content link — you'll use one of the following URLs:

基本下载的示例是:

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
    .executeMediaAndDownloadTo(outputStream);

关于android - 如何使用 Google Drive API 上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53989113/

相关文章:

java - 将目标 sdk 更新到 29 后导入已弃用的警告

android - 陷入无限的谷歌登录循环

android - 带有 Google Drive SDK 的 Play Store 应用程序始终显示 'Choose account' 弹出窗口

android - 如何获取要在android中下载的谷歌驱动器文件的url

android - 任何用于在 Eclipse 上的 Android 中裁剪图像的好库

android - ViewModel 类实现 LifecycleObserver 可以吗?

android - 线程并发 wait() 其他线程完成并继续其工作

android - 使用 setContentView 时,我看到系统墙纸一秒钟

android - 使用 Google Drive 备份和恢复 SQLite 数据库

android - 播放服务 11.6 : OAuth consent screen not showing when published