Google Calendar API安卓事件

时间:2017-03-28 10:20:45

标签: java android google-api google-calendar-api

我正在尝试使用Google Calendar API在Android应用中使用文件a(.pdf)创建一个事件:Create Events

public static void addAttachment(Calendar calendarService, Drive driveService, String calendarId,
    String eventId, String fileId) throws IOException {
  File file = driveService, android .files().get(fileId).execute();
  Event event = calendarService.events().get(calendarId, eventId).execute();

  List<EventAttachment> attachments = event.getAttachments();
  if (attachments == null) {
    attachments = new ArrayList<EventAttachment>();
  }
  attachments.add(new EventAttachment()
      .setFileUrl(file.getAlternateLink())
      .setMimeType(file.getMimeType())
      .setTitle(file.getTitle()));

  Event changes = new Event()
      .setAttachments(attachments);
  calendarService.events().patch(calendarId, eventId, changes)
      .setSupportsAttachments(true)
      .execute();
}

我复制了这个,但它不起作用,Android Studio输入红色getAlternateLink()和getTitle()没有重新协调,特别是行:

attachments.add(new EventAttachment()
      .setFileUrl(file.getAlternateLink())
      .setMimeType(file.getMimeType())
      .setTitle(file.getTitle()));

2 个答案:

答案 0 :(得分:0)

在云端硬盘中:v3不会退出getALternateLink()将版本更改为应用中的v2

//    compile('com.google.apis:google-api-services-drive:v3-rev64-1.22.0') {
//        exclude group: 'org.apache.httpcomponents'
//    }

把这个

compile('com.google.apis:google-api-services-drive:v2-rev123-1.18.0-rc'){
    exclude group: 'org.apache.httpcomponents'
}

答案 1 :(得分:0)

对于Drive API V3,您需要获取驱动器文件的元数据。首先获取Drive文件,然后获取Meta数据。元数据对象具有全部3个值。

Task<Metadata> metadataTask = getDriveResourceClient().getMetadata(driveFile.getDriveId().asDriveResource());

Tasks.await(metadataTask);
Metadata meta =  metadataTask.getResult();

Event event = mService.events().get(calendarId, eventId).execute();
List<EventAttachment> attachments = event.getAttachments();
if (attachments == null) {
    attachments = new ArrayList<>();
}

Event changes = new Event();
attachments.add(new EventAttachment()
        .setFileUrl(meta.getAlternateLink())
        .setMimeType(meta.getMimeType())
        .setTitle(meta.getTitle()));
changes.setAttachments(attachments);

mService.events()
        .patch(calendarId, eventId, changes)
        .setSupportsAttachments(true)
        .execute();