擷取附註和附件

以下範例顯示如何擷取記事及其附件:

REST

使用以下內容呼叫 media.download(): 附件下載名稱與 alt=media 網址參數。 alt=media 網址參數會告知伺服器系統下載的內容 例如文字、圖像和網路

如要取得附件的名稱,您必須先擷取記事

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);
    // A second call is required in order 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);
  }
}