Image captured from Sqlite gets black background in Imageview

Asked

Viewed 132 times

0

In my application, I have the need to capture an image that is on a basis SQLite in format byte (Blob) and then convert it to Bitmap in order to display it in a ImageView.

My problem is that the space of ImageView that remains horizontal is always with black background.

layout.xml

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

    <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/imageView1" 
        android:layout_weight="1" 
        android:src="@drawable/foto_default" /> 

</LinearLayout>

Conversion method:

private void setImage(){
    byte[] outImage = lista.get(position).getFoto(); 
    ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage); 
    Bitmap imagemConvertida = BitmapFactory.decodeStream(imageStream); 
    foto.setImageBitmap(imagemConvertida);
}

Can you help me?

  • Hello. Could you please edit your question with an image of how your ImageView is, the code of how you are doing this conversion and also the layout (.xml) in which your ImageView is?

1 answer

0


I was able to solve my problem. The problem was in the code that converts the Bitmap image to bytes. I was saving the image as JPEG and could not. The image had to be saved as PNG. Here’s the corrected code:

imageView.buildDrawingCache(); Bitmap imagemDoImageview = imageView.getDrawingCache(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); imagemDoImageview.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] imageInByte = stream.toByteArray();

Browser other questions tagged

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