InputStream inputstrem =getContentResolver().openInputStream(图片Uri);
//解码获取图片的宽高
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//只读取图片,不加载到内存中
BitmapFactory.decodeStream(inputstrem ,null,options);
//通过图片的宽高压缩图片,解决OOM~
options.inSampleSize = calculateInSampleSize(options,300,300);
options.inJustDecodeBounds = false;//加载到内存中
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;//采用抖动解码
//InputStream 被调用两次,第一次调用流被关闭清空了!!!因此需要重新打开流
inputstrem=getContentResolver().openInputStream(图片Uri); //关键代码,重新打开图片流,不加这一行代码 BitmapFactory.decodeStream返回的就是空null
Bitmap bmp = BitmapFactory.decodeStream(inputstrem , null, options);
/***绑定ImageView**/
Imageview1.setImageBitmap(bmp);
///calculateInSampleSize方法
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}