二次采样


二次采样
需要加权限读写权限
//加载本地图片
 BitmapFactory.Options options=new BitmapFactory.Options();
 options.inJustDecodeBounds=true;
 BitmapFactory.decodeFile("/storage/emulated/0/aaa.JPG",options);
 //获取图片的宽高
 int outWidth = options.outWidth;
 int outHeight = options.outHeight;
 //获取手机屏幕宽高
 Display display = getWindow().getWindowManager().getDefaultDisplay();
 int width = display.getWidth();
 int height = display.getHeight();
 //定义一个默认的比率值
 int scale=0;
 int scaleX=outWidth/width;
 int scaleY=outHeight/height;
 //得到一个大的比率值
 scale=scaleX>scaleY?scaleX:scaleY;
 //设置比例
 options.inSampleSize=scale;
 //加载图片内容
 options.inJustDecodeBounds=false;
 //按照比例进行压缩
 Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/aaa.JPG", options);
 iv.setImageBitmap(bitmap);

猜你喜欢

转载自blog.csdn.net/nideyida1/article/details/81000788