0

page:

<ui:repeat value="#{testController.photos}" var="item">
    <p:graphicImage value="#{item}"/>
</ui:repeat>

bean:

private TestEntity current;

public List getPhotos() {
    StreamedContent image;
    Collection rawPhotoCollection = current.getPhotoCollection();

    List<StreamedContent> imageList = new ArrayList<StreamedContent>(rawPhotoCollection);
    List<Photo> photoList = new ArrayList<Photo>(rawPhotoCollection);

    for (int i = 0; i < photoList.size(); i++) {
        byte[] data = photoList.get(i).getImage();
        ByteArrayInputStream is = new ByteArrayInputStream(data);
        image = new DefaultStreamedContent(is, "image/png");
        imageList.set(i, image);
    }

    return imageList;
}

TestEntity has a collection of Photo Entity. I casted the Photo Collection into an Array List of Photos. Photo has a column Image declared as BLOB. Created a Loop to retrieve each Image so i can convert it to a StreamedContent. Inserted each StreamedContent into imageList (a List of StreamedContent) returned the imageList and displayed it as p:graphicImage.

the problem is, the page gives me an icon of a broken image. it does not display the image. I am pretty sure that it has an image coz if i wont use ui:repeat and just display the selected Photo Entity using p:graphicImage, it displays the correct image.

something like:

page:

<p:graphicImage value="#{testController.firstImage}"/>

bean:

public StreamedContent getFirstImage() {
    Collection rawPhotoCollection = current.getPhotoCollection();
    List<Photo> photoList = new ArrayList<Photo>(rawPhotoCollection);
    byte[] data = photoList.get(0).getImage();
    ByteArrayInputStream is = new ByteArrayInputStream(data);
    StreamedContent image = new DefaultStreamedContent(is, "image/png");
    return image;
}

the above code works.

galao
  • 1,281
  • 8
  • 26
  • 50
  • 2
    possible duplicate of [How to use with DefaultStreamedContent in an ui:repeat?](http://stackoverflow.com/questions/10944673/how-to-use-pgraphicimage-with-defaultstreamedcontent-in-an-uirepeat) – BalusC Mar 25 '13 at 10:29
  • @BalusC hi, in reference of the link above, im confuse on this `private List imageIds; // +getter` on my database the images are saved as blob, how can i give them unique image identifiers? – galao Mar 26 '13 at 02:50
  • Just use whatever you already use to select a single and specific image from the DB. E.g. the PK column? – BalusC Mar 26 '13 at 02:54
  • 1
    ok thanks, now i have the list of IDs. now im confuse on what is ImageService of `Image image = service.find(Long.valueOf(id));` – galao Mar 26 '13 at 03:05

0 Answers0