ImageView Error can not be converted to Byte []

0

I'm putting together an application that you want to save the image to in SQLITE Bank. I am not able to proceed because the following error occurs ImageView cannot be converted to Byte[] I ask to be the most specific given my inexperience. Thanks

My code where the error occurred:

   Button btn1Salvar = (Button) findViewById(R.id.btSalvar);
    btn1Salvar.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Produto pro = new Produto();
            pro.setId(Integer.valueOf(edId.getText().toString()));
            pro.setDescricao(edDescricao.getText().toString());
            pro.setPrecoDeCusto(MonetaryMask.stringMonetarioToDouble(edPrecoDeCusto.getText().toString()));
            pro.setPercDeLucro(Double.valueOf(edPercDeLucro.getText().toString()));
            pro.setPrecoDeVenda(MonetaryMask.stringMonetarioToDouble(edPrecoDeVenda.getText().toString()));
            pro.setImagem(imgView);  // <-- aqui aparece o erro..sublinhado no imgView
    
asked by anonymous 23.01.2016 / 21:38

1 answer

2

I think the pro.setImagem() method expects a byte[] so it has to first convert the Bitmat that is in ImageView to byte[] .

Create a function that does this conversion:

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();
}

Use it this way:

pro.setImagem(convertImageViewToByteArray(imgView));
    
23.01.2016 / 21:57