当我的图片浏览页面中,加载所有的图片,当加载到大图片时候,出现OOM和 Bitmap too large to be uploaded into a texture (2448x3264, max=2048x2048)。
A、OOM:
现场:BitmapFactory.decodeFile(path);当通过一个Uri去构造一个Bitmap时,图片过大了。
一开始,我的处理方式:
// 获取这个图片的宽和高
int width = bm.getWidth();
int height = bm.getHeight();
// 定义预转换成的图片的宽度和高度
int newWidth = 400;
int newHeight = 400;
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
return bm;
这样需要事先获取Bitmap的宽高,那么首先需要构造一个Bitmap,而构造的时候,由于图片过大,出现OOM。
options有一个属性:boolean android.graphics.BitmapFactory.Options.inJustDecodeBounds
options.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(path, options);
/*
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
如果该属性设置为true,解析器将不会返回Bitmap,而是null。但是options.outWidth ,options.outHeight 的字段仍然被设置,方便调用者查询Bitmap的宽高。不需要为Bitmap的实际像素划分内存。
*/
拿到Bitmap的宽高后,设置为自己想要的值后,重新构造自己的Bitmap:
options.outWidth = 400;
options.outHeight = 400;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
*******************************************
上述方法可以得到满意的图片,但是Factory在decode的时候,仍然没有节约内存,使用下面三个属性,真正节约手机有限的内存:
options.inPreferredConfig = Bitmap.Config.RGB_565;//默认为ARGB_8888.
//以下两个字段需一起使用:
options.inPurgeable = true;//产生的位图将得到像素空间,如果系统gc,那么将被清空。当像素再次被访问,如果Bitmap已经decode,那么将被自动重新解码
options.inInputShareable = true;//位图可以共享一个参考输入数据(inputstream、阵列等)
options.inSampleSize = 4;//decode 原图1/4