加载大图片

来源:互联网 发布:前瞻网数据库会员 编辑:程序博客网 时间:2024/04/29 02:46

解决加载大图片出现的oom

<span style="white-space:pre"></span>//解析图片时需要使用到的参数都封装在这个对象里了    Options opt = new Options();    //不为像素申请内存,只获取图片宽高    opt.inJustDecodeBounds = true;    BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/dog.jpg", opt);    //拿到图片宽高    int imageWidth = opt.outWidth;    int imageHeight = opt.outHeight;        Display dp = getWindowManager().getDefaultDisplay();    //拿到屏幕宽高int screenWidth = dp.getWidth();    int screenHeight = dp.getHeight();        //计算缩放比例    int scale = 1;    int scaleWidth = imageWidth / screenWidth;    int scaleHeight = imageHeight / screenHeight;    if(scaleWidth >= scaleHeight && scaleWidth >= 1){    scale = scaleWidth;    }    else if(scaleWidth < scaleHeight && scaleHeight >= 1){    scale = scaleHeight;    }        //设置缩放比例    opt.inSampleSize = scale;    opt.inJustDecodeBounds = false;    Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opt);        ImageView iv = (ImageView) findViewById(R.id.iv);    iv.setImageBitmap(bm);


0 0