Bitmapfactory.decodestream () gets bitmap returns null

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.