码迷,mamicode.com
首页 > 其他好文 > 详细

BitmapFactory.decodeStream()获取bitmap返回null

时间:2014-12-26 16:55:42      阅读:849      评论:0      收藏:0      [点我收藏+]

标签:

正常的图片缩放代码如:

 ByteArrayOutputStream baos = new ByteArrayOutputStream();

arg1.compress(Bitmap.CompressFormat.JPEG, 100, baos);//arg1为传进来的原始bitmap

baos.toByteArray();

InputStream is = new ByteArrayInputStream(baos.toByteArray());

//进行缩放

BitmapFactory.Options newOpts = new BitmapFactory.Options();

// 开始读入图片,此时把options.inJustDecodeBounds 设回true了

newOpts.inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory.decodeStream(is,null,newOpts);// 此时返回bm为空

newOpts.inJustDecodeBounds = false;

int w = newOpts.outWidth;

int h = newOpts.outHeight;

// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为

float hh = 100f;// 这里设置高度为800f

float ww = 100f;// 这里设置宽度为480f

// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

int be = 1;// be=1表示不缩放

if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放

be = (int) (newOpts.outWidth / ww);

} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放

be = (int) (newOpts.outHeight / hh);

}

if (be <= 0)

be = 1;

newOpts.inSampleSize = 2;// 设置缩放比例

// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了

bitmap = BitmapFactory.decodeStream(is, null, newOpts);

最后取得bitmap竟然为空,百思不得起解,然后将bitmap获取方式改为BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, newOpts)就能获取到了,这个真难道是android的一个bug?



BitmapFactory.decodeStream()获取bitmap返回null

标签:

原文地址:http://my.oschina.net/u/1463920/blog/360901

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!