Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Drawable from a stream without resizing it?

Tags:

android

If I have the minimum target SDK for my app set to 4 then all calls to Drawable.createFromStream resize images.

e.g. if the source image is 480px wide and the Android device my app is running on has a density of 1.5 then the following code returns a BitmapDrawable with a width of 320

URL url = new URL("http://www.examples.com/something.png");

Drawable d = Drawable.createFromStream(url.openStream(), "something.png");

Is there a method to force it to be unchanged or specify the scale (ldpi/mdpi/hdpi etC) to return an image from an InputStream?

Edit: Solution from below.

Bitmap b = BitmapFactory.decodeStream(inputStream);
b.setDensity(Bitmap.DENSITY_NONE);
Drawable d = new BitmapDrawable(b);
like image 499
Johnny Avatar asked Sep 09 '11 12:09

Johnny


1 Answers

You can load image as Bitmap. This way its size will be unchanged.

BitmapFactory.decodeStream(...)

Or you can try BitmapDrawable(InputStream is) constructor. It is deprecated but I guess it should do the job.

like image 165
Dmitry Ryadnenko Avatar answered Sep 27 '22 19:09

Dmitry Ryadnenko