码迷,mamicode.com
首页 > 移动开发 > 详细

android 下载图片出现SkImageDecoder::Factory returned null,BitmapFactory.Options压缩

时间:2015-06-23 19:45:27      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

网上有很多说是因为没有采用HttpClient造成的,尼玛,我改成了HttpClient 请求图片之后还是会出现SkImageDecoder::Factory returned null,

但是直接使用

bitmap = BitmapFactory.decodeStream(is);  是正常的,但解决不了图片大内存溢出的问题

 

解决办法:

重新获取一次流,注意看代码(红色部分):

/**
     * 根据网络url获取bitmap对象
     * @param url
     * @param width  要获取的宽度
     * @param height 要获取的高度 防止内存溢出
     * @return
     */
    public static Bitmap returnBitMap(String url, int width,int height) {

        Bitmap bitmap = null;
        try {
            HttpGet httpRequest = new HttpGet(url);
            Log.d("returnbitmap", "url="+url);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
            InputStream is = bufferedHttpEntity.getContent();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true; //只获取图片的高宽
            int scale = 1;
            BitmapFactory.decodeStream(is,null,options);
            int w = options.outWidth;
            int h = options.outHeight;
            Log.d("returnbitmap", "w="+w+";h="+h+";width="+width+";height="+height);
            while(true)
            {
                if ((width>0 && w < width) || (height>0 && h < height))
                {
                    break;
                }
                w /= 2;
                h /= 2;
                scale *= 2;
            }
            options.inJustDecodeBounds = false;
            options.inSampleSize = scale;
            is = bufferedHttpEntity.getContent();//重新获取流
            bitmap = BitmapFactory.decodeStream(is,null,options);
            Log.d("returnbitmap", "bitmap="+bitmap+(bitmap==null?"bitmap is null":"bitmap is not null"));
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
         catch (OutOfMemoryError e) {  
             e.printStackTrace();  
         }  
        return bitmap;
    }

 

这样就可以正常下载并显示了,噢耶!!!

 

android 下载图片出现SkImageDecoder::Factory returned null,BitmapFactory.Options压缩

标签:

原文地址:http://www.cnblogs.com/feijian/p/4596095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!