• 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;
    }
  • 相关阅读:
    谈谈图片上传及canvas压缩的流程
    前端应该懂得初级Web分析指标
    java OPENCV 连通域, Imgproc.findContours 例子,参数说明
    [学习opencv]高斯、中值、均值、双边滤波
    Opencv 图像叠加 添加水印
    帧间提取水印
    opencv mat 转灰度图
    编写一条sql命令,sql删除没有中文的表
    使用JavaCV/OpenCV抓取并存储摄像头图像
    周掌柜
  • 原文地址:https://www.cnblogs.com/pzxnet/p/12691531.html
Copyright © 2020-2023  润新知