Android: How to display an image in its original s

2019-04-12 11:55发布

问题:

I create an bitmap image from byte array and display it in an ImageView. The android:layout_width and android:layout_height of the ImageView are set to wrap_content, and the android:scaleType is set to center. But the image is still scaled to the screen size.

Is there a way to display the image at its original size (just like using the android:src attribute directly)?

CODE:

create bitmap from byte array:

private Bitmap decodeBitmap(byte[] data) {
    return BitmapFactory.decodeByteArray(data, 0, data.length);
}

and layout of the ImageView:

<ImageView
    android:id="@+id/logo"
    android:scaleType="centerInside"
    android:adjustViewBounds="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />     

set image to ImageView:

mView.setImageBitmap(mBitmap);

回答1:

Please use this code if the stream is from resource

InputStream is = this.getResources().openRawResource(imageId1); 
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
ImageView myimage.setImageBitmap(originalBitmap); 
myimage.setScaleType(ScaleType.MATRIX);