How can I convert a String to Image?

0

I take an Image from the database but I can not because it says that I can not convert from String to Image

public static Pet showImage (Pet show) {

    String sql;

    java.sql.ResultSet rs;

    try {

        sql = "Select imagenMascota from TMascotas where idMascota = 2";

        rs = Conexion.getConector().ejecutarSQL(sql, true);

        mostrar.setImagenMascota(rs.getString("imagenMascota"));  

    } catch (Exception e) {

        System.out.println("No se encuentra " + e);

    }

    return mostrar;
}

And I'm saving it like this:

    Mascota imagenMasc = new Mascota();
    Image foto = null;
    File archivo;
    try {
        JFileChooser flcAbrirArchivo = new JFileChooser();
        flcAbrirArchivo.setFileFilter(new FileNameExtensionFilter("archivo de imagen", "jpg", "jpeg"));
        int respuesta = flcAbrirArchivo.showOpenDialog(this);
        if (respuesta == JFileChooser.APPROVE_OPTION) {

            archivo = flcAbrirArchivo.getSelectedFile();
            jTextField1.setText(archivo.getAbsolutePath());
            foto = getToolkit().getImage(jTextField1.getText());
            foto = foto.getScaledInstance(110, 110, 1);
            jLabel4.setIcon(new ImageIcon(foto));

        }
        imagenMasc.setImagenMascota(foto);

        Gestor.guardarImagen(imagenMasc);
    } catch (Exception ex) {
        Logger.getLogger(RegistrarMascota.class.getName()).log(Level.SEVERE, null, ex);
    }
    
asked by Joel López 24.08.2017 в 04:12
source

1 answer

0

Your error is that setImagenMascota , use the prototype setImagenMascota(java.awt.Image) (in some part of it), however you are sending a java.lang.String . To create an Image you need to first read the database data as java.io.InputStream , then create the Image by analyzing the byte stream

...
mostrar.setImagenMascota(
  javax.imageio.ImageIO.read(
    rs.getBinaryStream("imagenMascota")
  )
);
...
    
answered by 24.08.2017 в 04:37