网上有很多说是因为没有采用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;
}

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

最新文章

  1. 大叔最新课程~MVC核心技术剖析
  2. NoSQL数据库笔谈(转)
  3. bzoj1188 [HNOI2007]分裂游戏 博弈论 sg函数的应用
  4. [转载] 使用MySQL Proxy解决MySQL主从同步延迟
  5. iOS - Xcode 插件
  6. ADO.NET 基础
  7. javax.xml.ws.webserviceexception class do not have a property of the name
  8. xml学习总结(四)
  9. Delphi重载,覆盖,多态
  10. 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
  11. Linux显示系统的诊断信息
  12. Fluent Interface(流式接口)
  13. windows下 jdk1.7安装教程图解
  14. ASP.NET上传大文件报错,IIS7.0
  15. 【ARTS】01_12_左耳听风-20190128~20190203
  16. I2S接口介绍
  17. 【转】AlphaGo与人工智能
  18. Shiro系列(2) - 权限模型以及权限分配的两种方式
  19. The Go scheduler
  20. c++11 List 容器

热门文章

  1. 基本的Web控件二
  2. 软件工程 speedsnail 第二次冲刺8
  3. ASP.NET的学习之asp.net整体运行机制
  4. CSS3 column-rule-style 属性
  5. tomcat学习笔记2
  6. sublime配置问题
  7. Yii整合ZF2及soap实例
  8. ViewPage显示Fragment集合实现左右滑动并且出现tab栏--第三方开源--SlidingTabLayout和SlidingTabStrip实现
  9. 消息队列之RabbitMQ
  10. sql 子查询要命名