Error showing list of images that were recovered from BD

Asked

Viewed 37 times

0

Images are recovered normally, but do not appear in the gallery. NOTE: If I take a picture of the list and play in one p:graphicImage works normal.

Mbean:

package org.salomao.bean;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import org.salomao.controller.PostagemController;
import org.salomao.model.Midia;
import org.salomao.model.Postagem;
import org.salomao.model.Usuario;
import org.salomao.util.Utilitarios;

@Named
@SessionScoped
/**
 * @author rober
 *
 */
public class PostagemBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Usuario usuario;
private Postagem postagem;
private UploadedFile file;

// private byte[] ultimaimagem;

public PostagemBean() {
    // TODO Auto-generated constructor stub

    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(false);

    usuario = (Usuario) session.getAttribute("usuario");
    postagem = new Postagem();
}

@PostConstruct
public void novaPostagem() {
    postagem = new Postagem();
}

public StreamedContent mostarMidiaParam() {
    String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("image_id");

    return Utilitarios.getUtilitarios()
            .mostrarImagemDeByte(postagem.getMidia().get(Integer.valueOf(id)).getConteudo(), "", "");

}

public StreamedContent mostarMidia(Midia midia) {

    if (midia != null)
        return Utilitarios.getUtilitarios().mostrarImagemDeMidia(midia);
    else
        return null;

}

public List<Midia> mostarMidias() {

    return postagem.getMidia();

}

@Transactional
public List<StreamedContent> mostarMidiasS() {

    List<StreamedContent> content = new ArrayList<StreamedContent>();
    if ((postagem != null) && (postagem.getMidia() != null) && (postagem.getMidia().size() > 0))
        for (Midia midia : postagem.getMidia()) {
            content.add(Utilitarios.getUtilitarios().mostrarImagemDeMidia(midia));
        }

    return content;

}

public void handleFileUpload(FileUploadEvent fileUploadEvent) {
    // usuario.setImagem(fileUploadEvent.getFile().getContents());

    if (postagem.getMidia() == null) {
        List<Midia> lista = new ArrayList<Midia>();
        postagem.setMidia(lista);
    }
    Midia midia = new Midia();
    String nomeArquivo = fileUploadEvent.getFile().getFileName();
    midia.setExtencao(nomeArquivo.substring(nomeArquivo.lastIndexOf("."), nomeArquivo.length()).replace(".", ""));
    midia.setNome(nomeArquivo.replace("." + midia.getExtencao(), ""));
    midia.setPostagem(postagem);
    midia.setConteudo(fileUploadEvent.getFile().getContents());

    postagem.getMidia().add(midia);
}

public String postar() {

    postagem.setUsuario(usuario);
    postagem.setDataPostagem(LocalDateTime.now());
    try {
        PostagemController controller = new PostagemController();
        controller.setPostagem(postagem);
        controller.salvar();
        postagem = new Postagem();
        return "/pages/dash.faces?faces-redirect=true";
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // new Pos
    return "";
}

/**
 * @return the postagem
 */
public Postagem getPostagem() {
    return postagem;
}

/**
 * @param postagem
 *            the postagem to set
 */
public void setPostagem(Postagem postagem) {
    this.postagem = postagem;
}

/**
 * @return the file
 */
public UploadedFile getFile() {
    return file;
}

/**
 * @param file
 *            the file to set
 */
public void setFile(UploadedFile file) {
    this.file = file;
}
}

XHTML:

<ui:define name="conteudo">
    <h:form class="container">

        <h3 style="margin-top: 0" align="center">
            <p:outputLabel>Postagem</p:outputLabel>
        </h3>
        <div class="row">
            <p:outputLabel value="Tags" />
            <b:inputText tags="true" value="#{postagemBean.postagem.tags}" />
        </div>
        <div class="row">
            <p:outputLabel value="Politicos" />
            <p:autoComplete />
        </div>
        <div class="row">
            <p:outputLabel value="Conteudo" />
            <p:inputTextarea rows="10" cols="50" id="conteudo"
                value="#{postagemBean.postagem.conteudo}"
                placeholder="Enter your content" />
        </div>
        <div>
            <p:fileUpload fileUploadListener="#{postagemBean.handleFileUpload}"
                mode="advanced" auto="true" label="Enviar Arquvios.."
                update="galeria" />
            <p:galleria id="galeria" value="#{postagemBean.mostarMidiasS()}"
                effect="slide" effectSpeed="2000" var="image" panelWidth="300"
                panelHeight="300" showCaption="true" frameWidth="50"
                frameHeight="50">
                <p:graphicImage height="300" width="300" rendered="#{image != null}"
                    value="#{image}" cache="false"
                    title="1">


                </p:graphicImage>
            </p:galleria>

        </div>

        <div class="row">
            <div class="button">
                <p:commandButton action="#{postagemBean.postar()}"
                    class="waves-effect waves-light btn" value="Continuar" />
            </div>
        </div>
    </h:form>

</ui:define>

  • Did you see how it’s used in the showcase? https://www.primefaces.org/showcase/ui/multimedia/galleria.xhtml. It’s different from the strategy you’re using, there the Galleria value list is from the paths to the images and not the complete object.

  • discovered the error was just mark stream property="false" in p:graphicImage

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.