加载大图片报OOM错误
public void load(View view){ //图片太大会导致内存溢出 //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.big); //创建一个配置参数 BitmapFactory.Options opts = new Options(); opts.inJustDecodeBounds = true;//不真实解析位图,只是解析位图的宽高信息 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.big, opts); int width = opts.outWidth; int height = opts.outHeight; System.out.println("图片的宽度:"+width); System.out.println("图片的高度:"+height); int scale = 1; //计算缩放的比例 int scaleX = width/screenWidth; int scaleY = height/screenHeight; if(scaleX>scaleY && scaleY>=1){ scale = scaleX; }else if(scaleX<scaleY && scaleX>=1){ scale = scaleY; } opts.inSampleSize = scale; opts.inJustDecodeBounds = false;//设置好了宽高后,真实解析位图 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.big, opts); iv_show.setImageBitmap(bitmap); }
本文出自 “曾颐楠的播客” 博客,请务必保留此出处http://zengyinan.blog.51cto.com/9524976/1721476