0

Load images from database

asked 2013-06-03 14:55:58 +0800

posh gravatar image posh
3 2

updated 2013-06-03 19:11:54 +0800

I have the following method that creates an image and returns the path for the zul image tag ... But when I open the application on the browser or in the phone, the image is not displayed.. Can you help me?

ps: in the database the image is a blob entry .

public String getPath() { try{

        InputStream in = image.getBinaryStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        File temp = File.createTempFile("tempfile", ".png");
        path = temp.getAbsolutePath();

        OutputStream outputStream = new FileOutputStream(path);

        int bufferSize = 1024;
        int length = (int) image.length();

        byte[] buffer = new byte[bufferSize];

        while((length = in.read(buffer)) != -1)
        {
            out.write(buffer,0,length);
        }

        temp.deleteOnExit();
        out.writeTo(outputStream);
        in.close();
        out.close();

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

    //ImageUtils
    return path.replace('\\', '/');
}

I have this artigos.zul that contains the listbox with the database data, I use LoadArtigos.java to load the data and I use ControllerArtigos.java to control the events, in this case, when a row from the listbox is selected, ControllerArtigos.java listens to the event and open artigoDetail.zul that contains the detail of the selected item (the image included).

artigos.zul

<zk>

<borderlayout>

    <north vflex="min">
        <div vflex="1" hflex="1" sclass="topo">
            <hbox vflex="1" hflex="1" align="center">
                <hbox hflex="1" vflex="1">
                    <button image="/imagens/return.png" width="50px" height="50px" href="index.zul" />
                </hbox>
                <hbox hflex="1" vflex="1" align="end" pack="end">
                    <button image="/imagens/home.png" width="50px" height="50px" href="index.zul" />
                </hbox>
            </hbox>
        </div>
    </north>

    <center vflex="min">
        <div hflex="1" vflex="1">
            <listbox vflex="1" hflex="1" id="artigosListbox" apply="artigos.LoadArtigos, artigos.ControllerArtigos" >
                <listhead>
                    <listheader label="Código" />
                    <listheader label="Descrição" />
                </listhead>

                <template name="model">
                    <listitem>
                        <listcell label="${each.codigo }" />
                        <listcell label="${each.descricao }" />
                    </listitem>
                </template>
            </listbox>
        </div>  
    </center>

    <south vflex="min">
        <include src="sul.zul" />
    </south>

</borderlayout> 
</div>

</zk>

LoadArtigos.java

package artigos;

import java.util.List;

import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.select.SelectorComposer; import org.zkoss.zul.ListModelList; import org.zkoss.zul.Listbox;

public class LoadArtigos extends SelectorComposer<listbox> {

public void doAfterCompose(Listbox listbox)
{
    try {
        super.doAfterCompose(listbox);

        ArtigosDAO dao = new ArtigosDAO();
        List<Artigo> artigos = dao.read();

        listbox.setModel(new ListModelList<Artigo>(artigos));

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

ControllerArtigos.java

package artigos;

import java.util.HashMap; import java.util.Map; import java.util.Set;

import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.select.SelectorComposer; import org.zkoss.zk.ui.select.annotation.Listen; import org.zkoss.zk.ui.select.annotation.Wire; import org.zkoss.zul.*; import org.zkoss.zul.ext.Selectable;

public class ControllerArtigos extends SelectorComposer<component> {

@Wire
private Listbox artigosListbox;

@Wire
private Div global;

@Listen("onSelect = #artigosListbox")
public void showDetail()
{
    Set<Artigo> seleccao =  ((Selectable<Artigo>) artigosListbox.getModel()).getSelection(); 

    if(seleccao != null && !seleccao.isEmpty())
    {
        Map data = new HashMap();
        Artigo info = seleccao.iterator().next();

        data.put("CODIGO", info.getCodigo().toString());
        data.put("DESCRICAO", info.getDescricao());
        data.put("SRCIMAGEM", info.getCaminho());
        data.put("PRECOIVA", info.getPrecoIva().toString());

        global.setVisible(false);
        Executions.createComponents("artigoDetail.zul", null, data);

    }
}

}

artigoDetail

<zk> <borderlayout> <north vflex="min">

<hbox vflex="1" hflex="1" align="center"> <hbox hflex="1" vflex="1"> <button image="/imagens/return.png" width="50px" height="50px" href="artigos.zul"/> </hbox> <hbox hflex="1" vflex="1" align="end" pack="end"> <button image="/imagens/home.png" width="50px" height="50px" href="index.zul"/> </hbox> </hbox>
</north>

    <center vflex="min" autoscroll="true">
        <vbox hflex="1" vflex="1">
            <separator/>
            <hbox>
                Código:
                <textbox value="${arg.CODIGO }" />
            </hbox>

            <separator/>
            <hbox>
                Descrição:
                <textbox value="${arg.DESCRICAO }" />
            </hbox>
            <separator/>
            <hbox>
                Preço:
                <textbox value="${arg.PRECOIVA }" />
            </hbox>
            <separator/>
            <hbox>
                <image src="${arg.SRCIMAGEM }" />
            </hbox>
        </vbox>
    </center>

    <south vflex="min">
        <include src="sul.zul" />
    </south>
</borderlayout>

</zk>

delete flag offensive retag edit

Comments

What about your ZUl code how you bind the image tag from ZUL page?

sjoshi ( 2013-06-03 18:42:05 +0800 )edit

I've just edited the topic with your request :)

posh ( 2013-06-03 19:09:45 +0800 )edit

Can I make cast from blob to AImage when reading from the database ?

posh ( 2013-06-03 19:34:35 +0800 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2013-06-03 19:20:53 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

See if this Upload Images in Zk link will help you bind the image

link publish delete flag offensive edit
0

answered 2013-06-11 07:35:38 +0800

tmillsclare gravatar image tmillsclare
799 2 5 30

Hey posh,

Take a look at the AImage Javadoc here.

You can create the image using a File, String (location) or a stream along with a few others. I notice that you seem to already have a stream from the database, you could try and construct the AImage object using that then set the content of the image.

For information on setting the content of the image tag you can follow sjoshi's example and do a search through the docs.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-06-03 14:55:58 +0800

Seen: 111 times

Last updated: Jun 11 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More