【发布时间】:2020-09-23 05:23:20
【问题描述】:
我能够使用 Google Drive 提供的 API 成功地将我的图像添加到 Google Drive。
我现在可以将其检索回我的应用程序吗? 如果是,有人可以帮忙吗?
【问题讨论】:
-
查看此answer 可能对您有所帮助。
标签: android android-studio google-drive-android-api google-drive-realtime-api
我能够使用 Google Drive 提供的 API 成功地将我的图像添加到 Google Drive。
我现在可以将其检索回我的应用程序吗? 如果是,有人可以帮忙吗?
【问题讨论】:
标签: android android-studio google-drive-android-api google-drive-realtime-api
使用它从 Google Drive API 检索文件
https://www.googleapis.com/drive/v3/files/[FILEID]?key=[YOUR_API_KEY]'
如果您想要检索文件所需的全部代码,这里是:
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import java.io.IOException;
import java.io.InputStream;
// ...
public class MyClass {
// ...
/**
* Print a file's metadata.
*
* @param service Drive API service instance.
* @param fileId ID of the file to print metadata for.
*/
private static void printFile(Drive service, String fileId) {
try {
File file = service.files().get(fileId).execute();
System.out.println("Title: " + file.getTitle());
System.out.println("Description: " + file.getDescription());
System.out.println("MIME type: " + file.getMimeType());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
}
/**
* Download a file's content.
*
* @param service Drive API service instance.
* @param file Drive File instance.
* @return InputStream containing the file's content if successful,
* {@code null} otherwise.
*/
private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
// ...
}
【讨论】: