2014-09-29 111 views
1

后当我发布图片为我的墙由发布用图片facebook的页面restFb

FacebookType publish = fc.publish("me/photos", FacebookType.class, 
         BinaryAttachment.with(attachmentName, temporaryStream), 
         Parameter.with("message", "my myssage")); 

预期后创建。我可以在消息下看到一条消息和已发布的pictute。

但是当我想要做同样的事情,但在一些页面墙上,而不是用户墙。

FacebookType publish = fc.publish(pageId + "/photos", FacebookType.class, 
         BinaryAttachment.with(attachmentName, temporaryStream), 
         Parameter.with("message", "my myssage")); 

该帖子已发布,但我只能看到文本。不是图片。只有当我点击帖子的日期时才能看到图片。我还可以通过点击页面顶部的照片,然后点击“页面名称”照片标签来查看此照片。

有没有什么办法如何看到照片作为帖子的一部分,当我发布在网页上?

回答

3

我终于找到了解决方案。要将照片发布到具有普通“仅文字”帖子的部分,您需要将照片发布到墙上(您的用户墙而非页面墙),然后获取其链接,最后创建一个带有图片链接。

这里是示例代码:

FacebookType photo = fc.publish(pageid + "/photos" , FacebookType.class, 
      BinaryAttachment.with(photoName, photoInputStream)); 
Link photoLink = fc.fetchObject(photo.getId(), Link.class); 
FacebookType post = fc.publish(pageid + "/feed", FacebookType.class, 
      Parameter.with("message", "your message"),Parameter.with("type", "photo"), 
      Parameter.with("link", photoLink.getLink())); 

编辑:我忘了一件事。当您以后删除帖子时,您不会删除照片。要做到这一点,你必须手动完成。

3

你的答案工作一种享受,帮我无尽的金额,但是我发现我的解决方案,我并不需要

FacebookType post = fc.publish(pageid + "/feed", FacebookType.class, 
     Parameter.with("message", "your message"),Parameter.with("type", "photo"), 
     Parameter.with("link", photoLink.getLink())); 

,我会recive一个错误(我应该有复制给你看),但photoLink.getLink()在我的情况下返回null。

所以我更新的解决方案再次结束了

 FacebookType photo = client.publish(groupId + "/photos", 
      FacebookType.class, 
      BinaryAttachment.with(imageName), 
        imageAsBytes, 
        "image/png"), 
      Parameter.with("message", 
        message)); 

只是我的2美分,感谢您的帮助!

编辑

而且同时玩弄我注意到,在该解决方案,我来到了我创建一个FacebookType photo对象,这个对象没有得到任何地方使用,你只调用这是客户端的发布方法在您的程序中进一步创建。

我的最后一个类是低于,(我的应用程序从计算机拍摄图像,并给一组壁)

public void createImagePostInGroup(String message, String groupId, String imageFilePath){ 

    byte[] imageAsBytes = fetchBytesFromImage(imageFilePath); 

    DefaultFacebookClient client = 
     new DefaultFacebookClient(accessToken, Version.LATEST); 

    client.publish(groupId + "/photos", 
      FacebookType.class, 
      BinaryAttachment.with(imageFilePath.substring(imageFilePath.length()-34), 
        imageAsBytes, 
        "image/png"), 
      Parameter.with("message", 
        message)); 
} 

而我的图像被转换成一个字节数组在该方法中

private byte[] fetchBytesFromImage(String imageFile) { 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    byte fileContent[] = null; 

    try{ 

     BufferedImage image = ImageIO.read(new File(imageFile)); 

     ImageIO.write(image, "png", baos); 
     baos.flush(); 
     fileContent = baos.toByteArray(); 

    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
    return fileContent; 

} 

再次感谢您的帮助。