【Bitmap】亲测解决Bitmap出现 decoder->decode returned false 错误

来源:互联网 发布:数据分析师待遇 编辑:程序博客网 时间:2024/05/01 22:17

情景:

今天运营部的同事反馈了一个客户提出的问题,就是原先网络图片加载是没有问题的,但是现在显示不了了。于是追代码,发现服务器返回的json数据没问题,文件用浏览器打开也是没有问题的,很是纳闷,刚开始以为是连接超时,但是呢服务器运行状况是良好的,所以排除了。又想到图片可能是太大了OOM,但是图片是压缩过的返回图片大小也就100K+;终于在控制台被我抓到了

       decoder->decode returned false 错误

没错就是它,找到了源头。

解决方案:

参照:

http://stackoverflow.com/questions/4339082/android-decoder-decode-returned-false-for-bitmap-download

代码如下:

        

public static Bitmap loadImageFromUrl(String url) {        URL m;        InputStream i = null;        BufferedInputStream bis = null;        ByteArrayOutputStream out =null;        try {            m = new URL(url);            i = (InputStream) m.getContent();            bis = new BufferedInputStream(i,1024 * 8);            out = new ByteArrayOutputStream();            int len=0;            byte[] buffer = new byte[1024];            while((len = bis.read(buffer)) != -1){                out.write(buffer, 0, len);            }            out.close();            bis.close();        } catch (MalformedURLException e1) {            e1.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        byte[] data = out.toByteArray();        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);        //Drawable d = Drawable.createFromStream(i, "src");        return bitmap;    }

0 0
原创粉丝点击