正常的图片缩放代码如:android
ByteArrayOutputStream baos = new ByteArrayOutputStream();spa
arg1.compress(Bitmap.CompressFormat.JPEG, 100, baos);//arg1为传进来的原始bitmapcode
baos.toByteArray();orm
InputStream is = new ByteArrayInputStream(baos.toByteArray());图片
//进行缩放it
BitmapFactory.Options newOpts = new BitmapFactory.Options();io
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了map
newOpts.inJustDecodeBounds = true;bug
Bitmap bitmap = BitmapFactory.decodeStream(is,null,newOpts);// 此时返回bm为空float
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?