I’ve an image of 32×32, which I have put in drawable folder and the same image application downloads from server. But downloaded one looks bigger(more like 48×48) and pixelated.
Server Code ( C# )
MemoryStream tms = new MemoryStream();
image.Save(tms, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageData = tms.ToArray();
//send data to phone
Application Code ( Java )
ByteArrayInputStream memStream = new ByteArrayInputStream(imageData);
Drawable image = Drawable.createFromStream(memStream, ""); //big image and pixelated
memStream.close();
but if I load same file from Resource, its always smaller
Drawable image = getResources().getDrawable(R.drawable.image);
ImageView xml
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:scaleType="center" />
Edit :
Finally, setting the TargetDensity of BitmapDrawable worked!
Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length);
BitmapDrawable image = new BitmapDrawable(bmp);
image.setTargetDensity(bmp.getDensity());
Your image is rescaled depending on the device’s pixel density, try using
BitmapFactory.decodeStream(...)and wrap it usingnew BitmapDrawable(Bitmap)if you want a Drawable object.