Android Bitmap 内存溢出解决办法

//解决内存溢出情况
	private void SetImageSize(String path,ImageView imageView){
		BitmapFactory.Options opt = new BitmapFactory.Options(); 
        //这个isjustdecodebounds很重要                
        opt.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeFile(path, opt);
         
        //获取到这个图片的原始宽度和高度
         int picWidth  = opt.outWidth;
         int picHeight = opt.outHeight;
         
        //获取屏的宽度和高度
         WindowManager windowManager = ((Activity) context).getWindowManager();
         Display display = windowManager.getDefaultDisplay();
         int screenWidth = display.getWidth();
         int screenHeight = display.getHeight();
         
         //isSampleSize是表示对图片的缩放程度,比如值为2图片的宽度和高度都变为以前的1/2
         opt.inSampleSize = 1;
         //根据屏的大小和图片大小计算出缩放比例
         if(picWidth > picHeight){
                 if(picWidth > screenWidth)
                         opt.inSampleSize = picWidth/screenWidth;
         }
         else{
                 if(picHeight > screenHeight)

                         opt.inSampleSize = picHeight/screenHeight;
         }
         
         //这次再真正地生成一个有像素的,经过缩放了的bitmap
         opt.inJustDecodeBounds = false;
         bm = BitmapFactory.decodeFile(path, opt);
         
         //用imageview显示出bitmap
         imageView.setImageBitmap(bm);
	}

 移动开发网:www.chengdu135.com

你可能感兴趣的:(移动开发网)