读书人

5Mb图片转换为480*720的图片的内存溢出

发布时间: 2013-03-06 16:20:31 作者: rapoo

5Mb图片转换为480*720的图片的内存溢出问题
大侠们,求指点啊,有一张5Mb的图片,我处理完后,仍超内存,下面是我的代码,在线狂等中,雪地里跪求:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bm = getimage(path);
image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bm);
}

private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10;
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}

private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 720f;
float ww = 480f;
int 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 = be;
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);
}

[解决办法]
LRUCache用过吗?试试
http://developer.android.com/training/building-graphics.html
[解决办法]
Bitmap的Option也试试改一下,
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
以上之类。
[解决办法]
要不参考一下我这个 测试过5M的无压力
http://download.csdn.net/detail/piaohong/4978208

读书人网 >Android

热点推荐