风轻云淡

-------生活没有抱怨,只有自己的不努力,当才华撑不起梦想与野心,不断学习是你唯一的选择。没有伞的孩子必须努力奔跑...

导航

BitmapFactory.decodeStream返回null值 ,InputStream 被调用两次,第一次调用流被关闭清空了!!!

 
  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;
}

posted on 2020-04-13 15:13  淡然→笑  阅读(820)  评论(0编辑  收藏  举报