Mẫu sau đây cho thấy cách truy xuất một ghi chú và tệp đính kèm của ghi chú đó:
REST
Gọi media.download() bằng tên của tệp đính kèm được tải xuống và tham số URL alt=media.
Tham số URL alt=media cho máy chủ biết rằng một yêu cầu tải nội dung xuống đang được gửi.
Để lấy tên của tệp đính kèm, trước tiên, bạn phải truy xuất ghi chú.
Java
/**
* Gets and downloads the attachment of a note.
*
* @param note The note whose attachment will be downloaded.
* @throws IOException
*/
private void getNoteAttachment(Note note) throws IOException {
// First call is to get the attachment resources on the note.
List<Attachment> attachments =
keepService.notes().get(note.getName()).execute().getAttachments();
if (!attachments.isEmpty()) {
Attachment attachment = attachments.get(0);
String mimeType = attachment.getMimeType().get(0);
// Make a second call to download the attachment with the specified
// mimeType.
OutputStream outputStream =
new FileOutputStream("attachmentFile." + mimeType.split("/")[1]);
keepService
.media()
.download(attachment.getName())
.setMimeType(mimeType)
.executeMediaAndDownloadTo(outputStream);
}
}