图片二次采样有效防止因图片引起的OOM

OOM也就是我们经常遇到的:OutOfMenoryError 内存溢出异常

                        出现的原因那就是当加载的东西超出内存范围或内存不够用时会出现


                       

      解决方案:                      

                                   ExifInterface anInterface = new ExifInterface("图片路径")

                   //得到图片的宽和高
                                    int image_width = anInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
                                    int image_lenfth = anInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
                      //得到屏幕的宽和高
                                     WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
                                     int screen_width = manager.getDefaultDisplay().getWidth();
                                     int screen_height = manager.getDefaultDisplay().getHeight();
                        //通过图片的宽和高以及屏幕的宽和高得到一个压缩比的值
                                    int width = image_width/screen_width;
                                    int height = image_lenfth/screen_height;
                                    int score = 0;
                                    if(width>=height&&width>1){
                                        score=width;
                                    }else if(height>=width&&height>1){
                                        score=height;
                                    }
                            //返回Bitmap对象                                     
                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inSampleSize=score;
                            Bitmap bitmap = BitmapFactory.decodeFile("图片路径",options);
                    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值