我是Google Drive集成的新手。 我将图片保存在Google驱动器中,并通过以下方法获取了该文件。
File file = getDriveService().files().get(fileId).execute();
现在,当我尝试将此文件转换为InputStream并在HttpResponse中写入此InputStream时,文件将返回,但不会显示图像。
public InputStream convertGoolgeFileToInputStream( String fileId ) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
getDriveService().files().get(fileId).executeAndDownloadTo(outputStream);
InputStream in = new ByteArrayInputStream(outputStream.toByteArray());
return in;
//return getDriveService().files().get(fileId).executeAsInputStream();
}
答案 0 :(得分:0)
您可能想尝试检查下载示例Drive sample
private static void downloadFile(boolean useDirectDownload, File uploadedFile)
throws IOException {
// create parent directory (if necessary)
java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
MediaHttpDownloader downloader =
new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
downloader.setDirectDownloadEnabled(useDirectDownload);
downloader.setProgressListener(new FileDownloadProgressListener());
downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}