I am trying to create an initial-data.yml file for a Play! Framework application. Part of the web application involves images (for profile picture and such) and I want to store them as BLOBs in my MySQL database (I know this isn’t best practice, use of a file system will come later). I am wondering how I can put a “test” image in my YML file and how I can access it in the web app and display it. Any help would be appreciated. Below is what I have tried so far:
Part of YML:
Picture(Picture1):
picture: 010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101|image/png
Our Hibernate model has a picture as a byte[].
Here is how I am trying to access the image in the HTML:
#{if _post.picture}
<!-- display the picture -->
<p> Hey, a picture should go here.</p>
<img src="@{Application.showImage(_post.getPicture().getBinaryImage())}" alt="long islands for life"/>
#{/if}
Here is getBinaryImage()
private ByteArrayInputStream getBinaryImage() {
try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(byteImage));
ImageInputStream is = ImageIO.createImageInputStream(image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
return bais;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Right now I am getting a NullPointerException on the first line of this method because it seems that the image/byte[] is not being stored in the database. Any ideas how to get this appropriately stored in the database and then displaying on the webpage?
You should add your whole binary data in Base64 encoded form prepended by !!binary
This example is from Play Framework samples