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.