The normal image scaling code is as follows:
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Arg1.compress (Bitmap.CompressFormat.JPEG, BAOs),//arg1 for the original Bitmap passed in
Baos.tobytearray ();
InputStream is = new Bytearrayinputstream (Baos.tobytearray ());
To zoom
Bitmapfactory.options newopts = new Bitmapfactory.options ();
Start reading the picture, this time set the Options.injustdecodebounds back to True
Newopts.injustdecodebounds = true;
Bitmap Bitmap = Bitmapfactory.decodestream (is,null,newopts);//return BM to empty at this time
Newopts.injustdecodebounds = false;
int w = newopts.outwidth;
int h = newopts.outheight;
Now the mainstream phone is more 800*480 resolution, so the height and width we set to
float hh = 100f;//Here Set the height to 800f
float WW = 100f;//here set width to 480f
Scaling ratio. Because it is a fixed scale, only one of the data with high or wide is calculated
int be = 1;//be=1 means no scaling
if (W > H && w > ww) {//If the width is large, zoom according to the fixed width size
be = (int) (NEWOPTS.OUTWIDTH/WW);
} else if (W < h && h > hh) {//if height is scaled according to width fixed size
be = (int) (NEWOPTS.OUTHEIGHT/HH);
}
if (be <= 0)
be = 1;
Newopts.insamplesize = 2;//Setting the zoom ratio
Re-read the picture and note that the Options.injustdecodebounds has been set back to false at this time
Bitmap = Bitmapfactory.decodestream (is, null, newopts);
Finally get bitmap unexpectedly empty, think not escorts, and then will bitmap get the way to Bitmapfactory.decodebytearray (Baos.tobytearray (), 0, Baos.tobytearray (). Length, newopts) can be obtained, this really is a bug of Android?
Original: http://my.oschina.net/u/1463920/blog/360901
Bitmapfactory.decodestream () gets bitmap returns null