简体   繁体   中英

What is the best way to attach multiple images?

Several of my timeline item designs require multiple images, yet I am having difficulty attaching them all reliably. The timeline.insert function only seems to allow for one attachment and inserting attachments after the timeline item is inserted sometimes results in the images not being rendered.

I also tried using setAttachments on the timeline item itself but it didn't seem to actually upload the attachments when inserting the item. Using the code below I tend to get mixed results. Sometimes it works and other times it fails to render the image. There seems to be a correlation with how long I wait to view the notification after receiving it, if I view it too quickly it never fully renders.

Does anyone have any thoughts or suggestions on how I could overcome this or see anything I'm doing wrong?

//CardFactory.java - Create TimelineItem with attachment list
public static TimelineItem getConceptCard(String conceptImage) {
    TimelineItem timelineItem = new TimelineItem();
    timelineItem.setHtml("<article class=\"photo\">\n <img src=\"attachment:0\" width=\"100%\" height=\"100%\">\n <div class=\"photo-overlay\"/>\n <section>\n <p class=\"text-auto-size\">Test</p>\n </section>\n</article>\n");
    List<Attachment> attachments = new ArrayList<Attachment>();
    Attachment img1 = new Attachment();
    img1.setId("backImage");
    img1.setContentType("image/jpeg");
    img1.setContentUrl(WebUtil.buildStaticImgUrl("cardconcepts/" + conceptImage + ".JPG"));
    attachments.add(img1);
    timelineItem.setAttachments(attachments);
    timelineItem.setNotification(new NotificationConfig().setLevel("DEFAULT"));
    return timelineItem;
}




//MainServlet.java - Send TimelineItem on button press
} else if (req.getParameter("operation").equals("insertConceptCard")) {
    TimelineItem timelineItem = CardFactory.getConceptCard(req.getParameter("conceptCard"));
    MirrorClient.insertTimelineCard(credential, timelineItem);




//MirrorClient.java - Insert TimelineItem with multiple attachments
public static void insertTimelineCard(Credential credential, TimelineItem item) throws IOException {
    Mirror.Timeline timeline = getMirror(credential).timeline();
    TimelineItem timelineItem = timeline.insert(item).execute();
    for(Attachment TAttach : item.getAttachments()){
        InputStreamContent mediaContent = new InputStreamContent(TAttach.getContentType(), new URL(TAttach.getContentUrl()).openStream());
        timeline.attachments().insert(timelineItem.getId(), mediaContent).execute();
    }

I am not sure if it is possible given your requirements, but if the attachments are public images, you don't actually need to attach them. You can use the img tag with a normal http URL. My experience has been that these get fetched fairly quickly, are cached if you use them frequently, and render correctly even if they don't render immediately.

(Even if your requirements need to keep these more private, you may wish to use standard image fetching with some kind of nonce instead of trying to attach them. I realize this doesn't quite answer your question, but it may be a useful workaround.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM