关于BitmapFactory解析流的问题a

今天碰到了一个超级恶心的问题,BitmapFactory.decodeStream(bis,null,options)一直是返回NULL

问题是这样子的:

 1   InputStream is= response.body().byteStream();
 2                 Bitmap bm;
 3                 BitmapFactory.Options options=new BitmapFactory.Options();
 4                 options.inJustDecodeBounds=true;
 5 
 6                 BitmapFactory.decodeStream(is,null,options);
 7 
 8                 int screenWidth=getActivity().getWindowManager().getDefaultDisplay().getWidth();
 9                 int screenHeight=getActivity().getWindowManager().getDefaultDisplay().getHeight();
10 
11                 int widthScale=options.outWidth/screenWidth;
12                 int heightScale=options.outHeight/screenHeight;
13 
14 
15                 int scale=widthScale>heightScale?widthScale:heightScale;
16                 options.inJustDecodeBounds=false;
17                 options.inSampleSize=scale;
18                 try {
19                     bm=BitmapFactory.decodeStream(is,null,options);
20                     image.setImageBitmap(bm);
21                     is.close();
22 
23                 } catch (Exception e) {
24                     e.printStackTrace();
25                 }

第19行返回的位图始终为NULL,各种纠结百度(原谅我没有翻墙工具,吐槽下之前用的旗舰VPN,简直就是个黑店,买了一年的会员,居然特么倒闭了!!!)

发现因为之前的inputstream流已经被使用过了,导致指针往后移动,所以再次读取的时候就读不到数据了,

使用is.reset();就可以了,但是,这边还有个坑。。。。。。

一开始使用这个直接报IO异常了

后来发现,要想使用这个,首先,你的流 is.markSupported()必须返回true,

 InputStream is= response.body().byteStream();
                BufferedInputStream bis=new BufferedInputStream(is);//用BufferedInputStream包装Inputstream
                Bitmap bm;
                BitmapFactory.Options options=new BitmapFactory.Options();
                options.inJustDecodeBounds=true;

                BitmapFactory.decodeStream(bis,null,options);

                int screenWidth=getActivity().getWindowManager().getDefaultDisplay().getWidth();
                int screenHeight=getActivity().getWindowManager().getDefaultDisplay().getHeight();

                int widthScale=options.outWidth/screenWidth;
                int heightScale=options.outHeight/screenHeight;


                int scale=widthScale>heightScale?widthScale:heightScale;
                options.inJustDecodeBounds=false;
                options.inSampleSize=scale;
                try {
                    bis.reset();//重置
                    bm=BitmapFactory.decodeStream(bis,null,options);
                    image.setImageBitmap(bm);
                    is.close();
            
                } catch (Exception e) {
                    e.printStackTrace();
                }

说是还有其他的解决办法,将inputstream解析成字节数组,使用decodeByteArray来解析,我试了下貌似也没用。。。。。

更新:

用上面的方法,会有一个比较坑的情况,请求图片过多,发现reset()报错,catch住之后就显示不了图片了。

然后还是用decodeByteArray吧。。。。

将inputStream转成 byte[] 

 

 

    private  byte[] getByteArrayFromInputStream(InputStream is){
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int len=0;
        try {
            while ((len=is.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  bos.toByteArray();

    }

两次decode均使用decodeByteArray(),搞定!!!!弄死人的节奏啊。。。。。

 

转载于:https://www.cnblogs.com/lyysz/p/5898972.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值