Android中按图片像素缩放图片的一种方法。通过这种方式,可以按像素缩放图片,巧妙避免加载大图可能发生OOM的情况。
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//设置true后,位图并没有真正加载到内容,仅仅获取所必要的参数
mBitmap = BitmapFactory.decodeFile(path,options);//path为文件路径
options.inJustDecodeBounds = false;
int be = (int)(options.outHeight/ (float)200);//设置打开图片高度为200像素
if (be <= 0)
be = 1;
options.inSampleSize = be;
mDstBmp= BitmapFactory.decodeFile(path,options);
options.inJustDecodeBounds = false;//真正将图片加载到内存
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "图片打开错误",
Toast.LENGTH_SHORT).show();
}