Save an image of Imagemview to Postgres

Asked

Viewed 231 times

0

Hello, I am making a screen where the user takes photo of the error and sends in our program.

The routine loads the image of both the Camera and the Gallery and I put in Imageview, in this part everything happens well, but then if the user wants he can save the photo or change, and the problem is in saving the image in the Postgresql database.

I did the routine to save but it seems that it saves only the memory note not the bytes of the image, in the database is saved only 6 characters that seems to me to be the image note.

Thanks for your help.

//routine to convert loaded image in bytes

public byte[] convertImageViewToByteArray(ImageView image){
        Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        return stream.toByteArray();
    }

//table, ge_field file will receive the image

CREATE TABLE public.tb_ged
(
  ge_id integer NOT NULL DEFAULT nextval('tb_ged_ge_id_seq'::regclass),
  ge_tabela character varying NOT NULL,
  ge_idtabela integer NOT NULL,
  ge_descricao character varying NOT NULL,
  ge_observ character varying,
  ge_arquivo bytea,
  ge_deletado character varying(1),
  ge_tamanho integer,
  CONSTRAINT tb_ged_pkey PRIMARY KEY (ge_id)
)

//mount sql and give the input in the database

        Arquivo arquivo = new Arquivo();

        byte[] bImagem = convertImageViewToByteArray(img_sl_erro);
        arquivo.ge_tabela       = "tb_funcionario";
        arquivo.ge_idtabela     = 11;
        arquivo.ge_descricao    = "imagem atendimento";
        arquivo.ge_observ       = "";
        arquivo.ge_arquivo      = bImagem;
        arquivo.ge_tamanho      = bImagem.length;

    sql =
                    "insert into "+ Arquivo.table+"("+
                            Arquivo.FIELD2+", "+
                            Arquivo.FIELD3+", "+
                            Arquivo.FIELD6+", "+
                            Arquivo.FIELD7+", "+
                            Arquivo.FIELD8+", "+
                            Arquivo.FIELD11 +
                    ") values ("+
                            "'"+ arquivo.ge_tabela+"', "+
                            arquivo.ge_idtabela+", "+
                            "'"+ arquivo.ge_descricao+"', "+
                            "'"+ arquivo.ge_observ+"', "+
                            "'"+ arquivo.ge_arquivo+"', "+
                            arquivo.ge_tamanho+
                    ")";

    @Override
        protected Object doInBackground(String... params) {

            try{
                Class.forName("org.postgresql.Driver").newInstance();

                try{


                    conn = DriverManager.getConnection(Constantes.db_url_evolucao, Constantes.dg_usuario, Constantes.dg_senha);

                    //Utils.log(params[0]);
                    PreparedStatement preparedStatement = conn.prepareStatement(params[0]);
                    preparedStatement.executeUpdate();
                    preparedStatement.close();

                    return SUCESS;
                }catch(Exception erro){
                    log("POSTGRES Erro #2: "+erro);
                    message(context, "POSTGRES Erro #2: "+erro);
                    return ERROR;
                }
            }catch(Exception erro){
                log("POSTGRES Erro #1: "+erro);
                message(context, "POSTGRES Erro #2: "+erro);
                return ERROR;
            }
        }
  • I’ll test this way and then I’ll get back to you, thank you

  • 1

    @Carlosheuberger, it worked!!!!! I used preparedStatement.setBytes(7, .ge_file); it can convert your comment as an answer for me to mark it?

No answers

Browser other questions tagged

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