今天做开发的时候需要加载网络图片并压缩,但是并不是我想象的那样一帆风顺,今天就把我碰到问题以及解决问题的过程记录下来。
首先压缩图片,很简单么,网上的资料也很多,把代码贴上供大家参考
final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); options.inSampleSize = 2; options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options);上面的代码就是把本地图片压缩俩倍
之后按照正常思路开始压缩网络上加载的图片
final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, options); options.inSampleSize = 2; options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(is, null, options);;
解决办法
public Bitmap decodeSamplerBitmap(byte[] data, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(data, 0, data.length, options); options.inSampleSize = calcImage(options, reqHeight, reqWidth); options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(data, 0, data.length, options); }记录一下