• 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;
    }
  • 相关阅读:
    星空雅梦
    星空雅梦
    Navicat permium快捷键
    Mysql建表+创建索引
    mysql 常见ALTER TABLE操作
    mysql常用的索引种类
    xShell终端中文乱码-解决方法
    git删除本地分支和远程分支
    git版本回退
    log4j2配置文件log4j2.xml详解
  • 原文地址:https://www.cnblogs.com/pzxnet/p/12691531.html
Copyright © 2020-2023  润新知