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