标签:
BitmapFactory.decodeStreeam()方法,该方法会调用native层代码来创建bitmap(两个重载都会调用)BitmapFactory.Options参数的方法,改参数可指定生成bitmap的大小BitmapFactory.OptionsBitmapFactory.decodeStreeam()生成bitmap根据图片路径或URI打开输入流
InputStream is = getContentResolver().openInputStream(imageUri);获取屏幕或View尺寸 
如果能确定View尺寸则使用View尺寸,如果不能(比如动态调整的View、自适应的View等)则获取最接近该View的尺寸,实在不行就获取当前Activity的Window尺寸(比屏幕尺寸小)
 
WindowManager windowManager = getWindowManager(); 
Display defaultDisplay = windowManager.getDefaultDisplay(); 
defaultDisplay.getHeight(); 
defaultDisplay.getWidth(); 
 
view.getMeasuredWidth(); 
view.getMeasuredHeight(); 
根据目标尺寸生成BitmapFactory.Options
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = dstSize;使用options调用BitmapFactory.decodeStream()生成bitmap
Bitmap bitmap = BitmapFactory.decodeStream(is, null, option);InputStream is = null;
try {
    int screenWidth=getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight=getWindowManager().getDefaultDisplay().getHeight();
    int maxSize=Math.max(screenWidth,screenHeight);//以长边为准
    is = getContentResolver().openInputStream(imageUri);
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = maxSize;
    Bitmap bitmap = BitmapFactory.decodeStream(is, null, option);
    imageView.setImageBitmap(bitmap);
} catch (Exception e) {
    e.printStackTrace();
} 
try{
    if(is!=null)is.close();
}标签:
原文地址:http://blog.csdn.net/xmh19936688/article/details/51883393