public class OutofmemoryActivity extends Activity {
private ImageView iv;
private Display display;// 包装的是窗体宽高的信息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.iv);
display = getWindowManager().getDefaultDisplay();
}
public void click(View view) {
BitmapFactory.Options opts = new Options();
opts.inJustDecodeBounds = true;
// 告诉解析器 不要真的去解析图片 只是把图片的宽高信息给提供出来.
BitmapFactory.decodeFile("/sdcard/bbb.jpg", opts);
int width = opts.outWidth;
int height = opts.outHeight;
// 得到手机屏幕的宽高信息.
int windowwidth = display.getWidth();
int windowheigth = display.getHeight();
int scalex = width / windowwidth;
int scaley = height / windowheigth;
if (scalex > scaley && scaley > 1) { // 水平方向的缩放比例比较大
opts.inSampleSize = scalex;
}
if (scaley > scalex && scalex > 1) { // 竖直方向的缩放比例比较大
opts.inSampleSize = scaley;
}
opts.inJustDecodeBounds = false;
//告诉解析器 按照 opts.inSampleSize 比例真实的返回位图
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/bbb.jpg", opts);
iv.setImageBitmap(bitmap);
}
}