我正在尝试使用 Android 上的 Google 云端硬盘 api 上传文件
我在上面的链接中注册了 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:
- Download a file — files.get with
alt=media
file resource- Download and export a Google Doc — files.export
- Link a user to a file —
webContentLink
from the file resource
基本下载的示例是:
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
关于android - 如何使用 Google Drive API 上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53989113/