Imageview error cannot be converted to Byte[]

Asked

Viewed 40 times

0

I am mounting an application to which you want to save the image in the SQLITE Database. 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. Thank you

My code where the error occurs:

   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

1 answer

2


I believe that the method pro.setImagem() expecting a byte[] for this you must first convert the Bitmat which is on Imageview at 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));

Browser other questions tagged

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